斷點(diǎn)

          每天進(jìn)步一點(diǎn)點(diǎn)!
          posts - 174, comments - 56, trackbacks - 0, articles - 21

          導(dǎo)航

          <2010年5月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(5)

          隨筆分類(174)

          隨筆檔案(174)

          文章分類(21)

          文章檔案(21)

          好友連接

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          PropertyUtils

          Posted on 2010-05-30 09:51 斷點(diǎn) 閱讀(553) 評(píng)論(0)  編輯  收藏 所屬分類: Apache
          實(shí)體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個(gè)參數(shù)" );
              }

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

            
              
          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(
          "斷點(diǎn)");
                      
                      
          // 通過PropertyUtils的getProperty方法獲取指定屬性的值
                      Integer id = (Integer)PropertyUtils.getProperty(entity, "id");  
                      String name 
          = (String)PropertyUtils.getProperty(entity, "name");  
                      System.out.println(
          "id = " + id + "  name = " + name);  
                        
                      
          // 調(diào)用PropertyUtils的setProperty方法設(shè)置entity的指定屬性
                      PropertyUtils.setProperty(entity, "name""每天進(jìn)步一點(diǎn)");  
                      System.out.println(
          "name = " + entity.getName());  
                        
                      
          // 通過PropertyUtils的describe方法把entity的所有屬性與屬性值封裝進(jìn)Map中
                      Map map = PropertyUtils.describe(entity);  
                      System.out.println(
          "id = " + map.get("id"+ "  name = " + map.get("name"));  
                        
                            }
            
          }
            

          輸出:
          id = 1  name = 斷點(diǎn)
          name = 每天進(jìn)步一點(diǎn)
          id = 1  name = 每天進(jìn)步一點(diǎn)


          其它例子:
          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){//險(xiǎn)別信息
                                  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);
                 }

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 财经| 太白县| 东方市| 赞皇县| 庆阳市| 镇巴县| 稷山县| 玉林市| 合阳县| 汉沽区| 桦南县| 民县| 永州市| 永兴县| 广州市| 凤阳县| 丰城市| 襄垣县| 五家渠市| 航空| 碌曲县| 汨罗市| 奉贤区| 开远市| 连江县| 峨边| 临沂市| 紫云| 昌邑市| 报价| 双桥区| 井陉县| 宣城市| 巫山县| 澎湖县| 桦南县| 莲花县| 耒阳市| 永顺县| 沈丘县| 蓬莱市|