将request.getParameterMap()转换成可操作的普通Map


在java web项目中虽然可以通过request.getParameterMap()很轻松的获得参数Map,但得到的Map和普通Map是不一样的,是被锁定的,不能像操作常规Map那样进行put、get等操作,该方法将得到参数Map返回为可操作的普通Map
 
标签:  Java  request  getParameterMap  Anynote
 

代码片段(1)[全屏查看所有代码]

1. [代码][Java]代码     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
  * 从request中获得参数Map,并返回可读的Map
  *
  * @param request
  * @return
  */
@SuppressWarnings ( "unchecked" )
public static Map getParameterMap(HttpServletRequest request) {
     // 参数Map
     Map properties = request.getParameterMap();
     // 返回值Map
     Map returnMap = new HashMap();
     Iterator entries = properties.entrySet().iterator();
     Map.Entry entry;
     String name = "" ;
     String value = "" ;
     while (entries.hasNext()) {
         entry = (Map.Entry) entries.next();
         name = (String) entry.getKey();
         Object valueObj = entry.getValue();
         if ( null == valueObj){
             value = "" ;
         } else if (valueObj instanceof String[]){
             String[] values = (String[])valueObj;
             for ( int i= 0 ;i<values.length;i++){
                 value = values[i] + "," ;
             }
             value = value.substring( 0 , value.length()- 1 );
         } else {
             value = valueObj.toString();
         }
         returnMap.put(name, value);
     }
     return returnMap;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM