斷點

          每天進步一點點!
          posts - 174, comments - 56, trackbacks - 0, articles - 21

          PropertyUtils

          Posted on 2010-05-30 09:51 斷點 閱讀(558) 評論(0)  編輯  收藏 所屬分類: Apache
          實體bean。
          package com.ztf;

          public class Entity {
              
          private Integer id;
              
          private String name;
              
              
          public void sayHello(){
                  System.out.println(
          "sayHello()---> 無參");
              }

              
              
          public void sayHello(String s){
                  System.out.println(
          "sayHello()---> 有1個參數" );
              }

              
              
          public void sayHello(Integer a,Integer b){
                  System.out.println(
          "sayHello()---> 有2個參數");
              }

            
              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
          public Integer getId() {
                  
          return id;
              }

              
          public void setId(Integer id) {
                  
          this.id = id;
              }

          }


          package com.ztf;
          import java.util.Map;

          import org.apache.commons.beanutils.MethodUtils;
          import org.apache.commons.beanutils.PropertyUtils;

          public class TestPropertyUtils {  
                  
          public static void main(String[] args) throws Exception{  
                        
                      Entity entity 
          = new Entity();  
                      entity.setId(
          1) ;
                      entity.setName(
          "斷點");
                      
                      
          // 通過PropertyUtils的getProperty方法獲取指定屬性的值
                      Integer id = (Integer)PropertyUtils.getProperty(entity, "id");  
                      String name 
          = (String)PropertyUtils.getProperty(entity, "name");  
                      System.out.println(
          "id = " + id + "  name = " + name);  
                        
                      
          // 調用PropertyUtils的setProperty方法設置entity的指定屬性
                      PropertyUtils.setProperty(entity, "name""每天進步一點");  
                      System.out.println(
          "name = " + entity.getName());  
                        
                      
          // 通過PropertyUtils的describe方法把entity的所有屬性與屬性值封裝進Map中
                      Map map = PropertyUtils.describe(entity);  
                      System.out.println(
          "id = " + map.get("id"+ "  name = " + map.get("name"));  
                        
                            }
            
          }
            

          輸出:
          id = 1  name = 斷點
          name = 每天進步一點
          id = 1  name = 每天進步一點


          其它例子:
          import org.apache.commons.beanutils.PropertyUtils;
          List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
          for(BaseGrpMemberVO member : MemberVO){
                    Map m = new HashMap();
                    for(Map.Entry<String, String> entry: fieldMap.entrySet()){
                              String key = entry.getKey();
                              fieldList.add(key); //記錄字段名
                              String[] keyArray = key.split("\\.");
                              if(keyArray.length == 2){//成員信息
                                  Object o =PropertyUtils.getProperty(member, keyArray[1]);
                                  m.put(key, o==null?"":o.toString());
                              }else if(keyArray.length == 3){//險別信息
                                  List<BaseGrpCvrgVO> cvrgVoList = mgr.getRelCvrgById(member.getCPkId());
                                  String cvrgNo = keyArray[0];
                                  for(BaseGrpCvrgVO cvrgVo : cvrgVoList){
                                      if(cvrgNo.equals(cvrgVo.getCCvrgNo())){
                                          Object o =PropertyUtils.getProperty(cvrgVo, keyArray[2]);
                                          m.put(key, o==null?"":o.toString());
                                      }
                                  }   
                              }
                          }
                    data.add(m);
                 }

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


          網站導航:
           
          主站蜘蛛池模板: 交口县| 增城市| 荣昌县| 宁晋县| 容城县| 陇川县| 光山县| 温宿县| 东乡县| 兴海县| 武宣县| 陇川县| 青神县| 昭通市| 铅山县| 宾阳县| 隆昌县| 鱼台县| 瑞金市| 商河县| 满城县| 贞丰县| 宜城市| SHOW| 永吉县| 宝山区| 手机| 大邑县| 乌拉特后旗| 信宜市| 康平县| 孟州市| 读书| 高青县| 肇庆市| 贵州省| 白沙| 葫芦岛市| 枣庄市| 三明市| 分宜县|