Map接口的使用。。。今天遇到的问题。。

类别:Java 点击:0 评论:0 推荐:

  /**
    * 从请求中取出所有参数列表,
    * 然后和工作项id一起保存到数据库中
    * @param req HttpServletRequest
    */
   protected void doSaveParameters(HttpServletRequest req){

     Map pMap = req.getParameterMap();
     System.out.println("zqbtest*******Map:"+pMap);
     Iterator entries = pMap.keySet().iterator();
     System.out.println("zqbtest*****Iterator::"+entries);
     while(entries.hasNext()){
       Map.Entry entry = (Map.Entry)entries.next();//此处出错!!!
       String key = (String)entry.getKey();
       String value = (String)entry.getValue();
       mtst.insertSaveParameter(workItemId,key,value);
     }

   }


后台打印信息:

zqbtest*******Map:{reassign=[Ljava.lang.String;@6e9cc3, fxcfh=[Ljava.lang.String
;@75d174, bsize=[Ljava.lang.String;@a4a32, readonlysendpersons=[Ljava.lang.Strin
g;@d539f, specialsendstep=[Ljava.lang.String;@6d0085, nextsteppersons=[Ljava.lan
g.String;@3d4817, fxfba=[Ljava.lang.String;@47e54e, specialsendpersons=[Ljava.la
ng.String;@1f91ac, fxfaaX=[Ljava.lang.String;@14f467, Unknown5copy=[Ljava.lang.S
tring;@39d072, Unknown3=[Ljava.lang.String;@2422d9}
zqbtest*****Iterator::java.util.HashMap$HashIterator@6c12f3


抛出异常如下:
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling

this request.

exception

java.lang.ClassCastException: java.lang.String
 at com.neusoft.fd.processor.WFProcessor.doSaveParameters(WFProcessor.java:1149)

程序经过修改后的代码:

   /**
    * 从请求中取出所有参数列表,
    * 然后和工作项id一起保存到数据库中
    * @param req HttpServletRequest
    */
   protected void doSaveParameters(HttpServletRequest req){

     Map pMap = req.getParameterMap();
    
     Iterator entries = pMap.entrySet().iterator();
    
     while(entries.hasNext()){

       Map.Entry entry = (java.util.Map.Entry) entries.next();
    
       String key = (String) entry.getKey();
      
       String[] values = (String[])entry.getValue();
       for (int i = 0; i < values.length; i++) {
         String value = values[i];
    
         mtst.insertSaveParameter(workItemId, (String) key, value);
       }
       //String value = (String)pMap.get(key);
      
     }

   }

错误的原因如下:
1、     Iterator entries = pMap.entrySet().iterator();
要用entrySet()取出Iterator;
2、entries.next() 得出的是java.util.Map.Entry这个是Map接口的内部接口;
    它的getValue方法,返回的是一个String[];


本文地址:http://com.8s8s.com/it/it14167.htm