afunms

          My Software,My Dream—Forge a more perfect NMS product.

          new framework(4)--fetch request parameters value via reflect

          最討厭就是寫一大堆request.getParameter,新架構中利用反射自動提取request中的參數值,
          然后把它們封閉成一個dto,真是太爽了。

           

              protected DtoInterface extractData(Class<? extends DtoInterface> clazz)
              
          {
                  DtoInterface dto 
          = null;
                  
          try
                  
          {
                      dto 
          = clazz.newInstance();
                      Method[] methods 
          = clazz.getMethods();
                      
          for(Method method:methods)
                      
          {
                          
          if(!method.getName().startsWith("set")) continue;
                          
                          String paraValue 
          = getParaValueByMethodName(method.getName().substring(3));
                          
          if(paraValue==nullcontinue;
                          
                          Class
          <?>[] types = method.getParameterTypes();
                          
          if(types==null || types.length==0)
                              
          continue;
                          
          //                System.out.println("methodName=" + method.getName());
          //                System.out.println("para=" + method.getName().substring(3));
          //                System.out.println("paraValue=" + paraValue);
                          
                          
          if(types[0].getName().equals("int"))
                             method.invoke(dto,Integer.parseInt(paraValue));
                          
          else if(types[0].getName().equals("long"))    
                             method.invoke(dto,Long.parseLong(paraValue));
                          
          else if(types[0].getName().equals("double"))    
                              method.invoke(dto,Double.parseDouble(paraValue));
                          
          else if(types[0].getName().equals("float"))    
                               method.invoke(dto,Float.parseFloat(paraValue));
                          
          else
                             method.invoke(dto,paraValue); 
          //String    
                      }

                  }

                  
          catch(Exception e)
                  
          {
                      SysLogger.error(
          "BaseAction.extractData()",e);    
                  }

                  
          return dto;
              }

              
              
          private String getParaValueByMethodName(String para)
              
          {
                  String result 
          = null;
                  
          for (Enumeration em = request.getParameterNames() ; em.hasMoreElements() ;)
                  
          {
                      String name 
          = (String)(em.nextElement());
                      String temp 
          = name.replaceAll("_","");
                      
          if(para.equalsIgnoreCase(temp))
                      
          {
                          result 
          = request.getParameter(name);
                          
          break;
                      }

                  }

                  
          return result;
              }

           

          posted on 2007-05-04 20:56 afunms 閱讀(133) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 沁源县| 青岛市| 石台县| 婺源县| 阜宁县| 和田县| 邯郸县| 林口县| 邵阳市| 大姚县| 屯留县| 迁西县| 溧阳市| 朝阳市| 杭锦旗| 三河市| 左云县| 西安市| 万盛区| 广昌县| 海林市| 昌宁县| 新郑市| 松桃| 博白县| 大安市| 古浪县| 登封市| 祥云县| 吴桥县| 祁阳县| 旬阳县| 溧水县| 洛宁县| 宕昌县| 甘谷县| 张家川| 高清| 高淳县| 六安市| 光泽县|