欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取HttpServletRequest中的所有参数方法

程序员文章站 2024-03-07 15:32:51
...

在controller中创建一个空Map然后将该map和request传到下面的方法中:

public static void putParametersIntoMap(HttpServletRequest request ,Map<String, Object> paramMap) {
    Enumeration<String> en = request.getParameterNames();
    try {
        while (en.hasMoreElements()) {
            String nms = en.nextElement();
            if (nms.endsWith("[]")) {
                String[] as = request.getParameterValues(nms);
                if (as != null && as.length != 0 && !Arrays.toString(as).equals("[]")) {
                    paramMap.put(nms.replace("[]", ""), Arrays.asList(as));
                }
            } else {
                String[] as = request.getParameterValues(nms);
                if (as.length == 1) {
                    paramMap.put(nms, as[0]);
                } else if (as.length > 1) {
                    paramMap.put(nms, Arrays.asList(as));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}