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

📄 paramshelper.java

📁 欢迎使用 FastJsp 开发框架! 编译说明: * 若要生成Api Javadoc文档
💻 JAVA
字号:
package com.onetsoft.fastjsp;

import com.onetsoft.fastjsp.request.PageData;
import com.onetsoft.fastjsp.util.Constants;
import com.onetsoft.fastjsp.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.Map;

public class ParamsHelper {

    public static String getParameter(Map parametersMap, String name) {
        Object o = parametersMap.get(name);
        if (o == null) return null;
        if (o instanceof Object[]) {
            Object[] a = (Object[]) o;
            if (a.length == 0) return StringUtils.EMPTY;
            return a[0] == null ? null : a[0].toString();
        } else {
            return o.toString();
        }
    }

    public static String[] getParameterValues(Map parametersMap, String name) {
        Object o = parametersMap.get(name);
        if (o instanceof String[]) {
            return (String[]) o;
        }
        if (o instanceof Object[]) {
            Object[] a = (Object[]) o;
            String[] row = new String[a.length];
            Object v;
            for (int i = 0; i < row.length; i++) {
                v = a[i];
                row[i] = v == null ? null : v.toString();
            }
            return row;
        } else
            return o != null ? new String[]{o.toString()} : null;
    }

    /**
     * 解析非上传http请求所包含的参数
     * 方便{@link PageData}直接取得参数
     *
     * @param request
     * @param parametersMap
     */
    static void decodeRequestParameterMap(HttpServletRequest request, Map parametersMap, String enc) {
        boolean methodPost = StringUtils.METHOD_POST.equalsIgnoreCase(request.getMethod());
        Map map = request.getParameterMap();
        if (map.isEmpty()) return;
        Map.Entry e;
        String[] a;
        for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
            e = (Map.Entry) iter.next();
            a = (String[]) e.getValue();
            if (a.length == 0) {
                parametersMap.put(e.getKey(), StringUtils.EMPTY);
            } else if (a.length == 1) {
                try {
                    // Debug.debug("before/after"+a[0]+"/"+ new String(a[0].getBytes(Constants.ISO_8859_1), enc));

                    /* 经测试 URL 参数似乎都是 8859_1 方式传递。*/
                    //    parametersMap.put(e.getKey(), new String(a[0].getBytes(Constants.ISO_8859_1), enc)); //url
                    if (methodPost)
                        parametersMap.put(e.getKey(), URLDecoder.decode(a[0], enc));        // post request
                    else
                        parametersMap.put(e.getKey(), new String(a[0].getBytes(Constants.ISO_8859_1), enc)); //url request

                } catch (/*UnsupportedEncoding*/Exception e1) {
                    parametersMap.put(e.getKey(), a[0]);
                }
            } else {
                for (int i = 0; i < a.length; i++) {
                    try {
                        if (methodPost)
                            a[i] = URLDecoder.decode(a[i], enc);
                        else
                            a[i] = new String(a[i].getBytes(Constants.ISO_8859_1), enc);
                    } catch (UnsupportedEncodingException e1) {
                        e1.printStackTrace();
                    }
                }
                parametersMap.put(e.getKey(), a);
            }
        }

        //  Debug.debug("ParamsHelper Line:74.====== 结束 ==========.... Leave Out decodeRequestParameterMap()");
    }

}

⌨️ 快捷键说明

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