afunms

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

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

          最討厭就是寫一大堆request.getParameter,新架構(gòu)中利用反射自動提取request中的參數(shù)值,
          然后把它們封閉成一個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)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 江陵县| 垦利县| 比如县| 紫金县| 抚远县| 苍山县| 金坛市| 安平县| 瑞昌市| 五河县| 加查县| 桃江县| 沁阳市| 湖南省| 五河县| 临澧县| 中阳县| 海阳市| 平罗县| 广灵县| 周宁县| 荥经县| 阜康市| 昌图县| 凤阳县| 娄底市| 万年县| 凌源市| 临西县| 峨山| 扎兰屯市| 常熟市| 西吉县| 滕州市| 满城县| 罗平县| 奉化市| 万盛区| 金坛市| 枝江市| 大埔区|