⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericparamutil.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/GenericParamUtil.java,v 1.6.2.1 2006/07/10 12:55:29 phuongpdd Exp $
 * $Author: phuongpdd $
 * $Revision: 1.6.2.1 $
 * $Date: 2006/07/10 12:55:29 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2006 by MyVietnam.net
 *
 * All copyright notices regarding MyVietnam and MyVietnam CoreLib
 * MUST remain intact in the scripts and source code.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Correspondence and Marketing Questions can be sent to:
 * info at MyVietnam net
 *
 * @author: Minh Nguyen  
 * @author: Mai  Nguyen  
 */
package net.myvietnam.mvncore.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import net.myvietnam.mvncore.MVNCoreResourceBundle;
import net.myvietnam.mvncore.exception.BadInputException;
import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
import net.myvietnam.mvncore.web.GenericRequest;

public final class GenericParamUtil {

    private GenericParamUtil() { // prevent instantiation
    }

    //private static String contextPath = (new ParamOptions()).contextPath;
    //private static String serverPath = (new ParamOptions()).serverPath;//@todo combine 2 line to a static block

    private static DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");

    public static String getParameter(GenericRequest request, String param) {

        String ret = request.getParameter(param);
        if (ret == null) ret = "";
        return ret.trim();
    }

    public static String getParameterFilter(GenericRequest request, String param) {
        return DisableHtmlTagFilter.filter(getParameter(request, param));
    }
    
    //获取表单,URL参数,
    public static String getParameter(GenericRequest request, String param, boolean checkEmpty)
        throws BadInputException {    	
        String ret = request.getParameter(param);
        if (ret == null) ret = "";
        ret = ret.trim();        
        if ( checkEmpty && (ret.length() == 0) ) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_allow_to_be_empty", new Object[] {DisableHtmlTagFilter.filter(param)});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static String getParameterFilter(GenericRequest request, String param, boolean checkEmpty)
        throws BadInputException {
        return DisableHtmlTagFilter.filter(getParameter(request, param, checkEmpty));
    }

    /** @todo review this method */
    public static String getParameterSafe(GenericRequest request, String param, boolean checkEmpty)
        throws BadInputException {

        String ret = getParameter(request, param, checkEmpty);
        if ( (ret.indexOf('<') != -1) ||
             (ret.indexOf('>') != -1)) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.parameter_safe", new Object[] {DisableHtmlTagFilter.filter(param)});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static int getParameterInt(GenericRequest request, String param)
        throws BadInputException {

        String inputStr = getParameter(request, param, true);
        int ret;
        try {
            ret = Integer.parseInt(inputStr);
        } catch (NumberFormatException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "int"});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static int getParameterUnsignedInt(GenericRequest request, String param)
        throws BadInputException {

        int retValue = getParameterInt(request, param);
        if (retValue < 0) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object[] {DisableHtmlTagFilter.filter(param)});
            throw new BadInputException(localizedMessage);
        }
        return retValue;
    }

    public static int getParameterInt(GenericRequest request, String param, int defaultValue)
        throws BadInputException {

        String inputStr = getParameter(request, param, false);
        if (inputStr.length() == 0) {
            return defaultValue;
        }
        int ret;
        try {
            ret = Integer.parseInt(inputStr);
        } catch (NumberFormatException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "int"});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static int getParameterUnsignedInt(GenericRequest request, String param, int defaultValue)
        throws BadInputException {

        int retValue = getParameterInt(request, param, defaultValue);
        if (retValue < 0) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object[] {DisableHtmlTagFilter.filter(param)});
            throw new BadInputException(localizedMessage);
        }
        return retValue;
    }

    public static long getParameterLong(GenericRequest request, String param)
        throws BadInputException {

        String inputStr = getParameter(request, param, true);
        long ret;
        try {
            ret = Long.parseLong(inputStr);
        } catch (NumberFormatException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "long"});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static long getParameterLong(GenericRequest request, String param, long defaultValue)
        throws BadInputException {

        String inputStr = getParameter(request, param, false);
        if (inputStr.length() == 0) {
            return defaultValue;
        }

        long ret;
        try {
            ret = Long.parseLong(inputStr);
        } catch (NumberFormatException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "long"});
            throw new BadInputException(localizedMessage);
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -