1. 程式人生 > >獲取HttpServletRequest中的所有引數方法

獲取HttpServletRequest中的所有引數方法

在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();
    }
}