Jakarta Commons BeanUtils

來源:互聯網
上載者:User
轉載: http://www.blogjava.net/Good-Game/archive/2007/08/10/135739.html Jakarta Commons BeanUtils 學習

參考:http://www.duduwolf.com/wiki/2007/296.html
      http://www.chinaitpower.com/A/2005-07-03/150232.html
1)普通的Bean處理  不管是什麼Set參數為3個 ( 對象本身, 屬性名稱或屬性內位置, 值 ) //list的add有點特別
                            Get參數為2個( 對象本身, 屬性名稱或屬性內位置 )
  

        Employee em = new Employee();  //String name; String[] ss;  Map map; List list;
        BeanUtils.setProperty(em,"name","liukaiyi");  //String set
        BeanUtils.setProperty(em,"ss",new String[]{"1","2","3"}); //String[] set
           BeanUtils.setProperty(em,"ss[2]","google");  //String[2] set

        BeanUtils.setProperty(em,"map",new HashMap());   //Map set 
            BeanUtils.setProperty(em,"map(key)","value");  //Map.put(Key,Value)
            
        BeanUtils.setProperty(em,"list",new ArrayList( Arrays.asList( new Object[20] ) )); //List有點特別
            BeanUtils.setProperty(em,"list[0]","list");       //不可以直接添加???
            
        BeanUtils.setProperty(em,"avg","23");       //int set
                
        
        System.out.println( BeanUtils.getSimpleProperty(em,"name") );   
        System.out.println( BeanUtils.getProperty(em,"ss[2]") );
        System.out.println( BeanUtils.getProperty(em,"map(key)") );
        
        System.out.println( BeanUtils.getProperty(em,"list[0].class") );  // 取的是 ==list.get(0).getClass()
        
        System.out.println( BeanUtils.getProperty(em,"avg") ); 

2)動態屬性

 //定義動態屬性集
 DynaProperty[] props = new DynaProperty[]{
    new DynaProperty("address", java.util.Map.class),
    new DynaProperty("subordinate", mypackage.Employee[].class),
    new DynaProperty("firstName", String.class),
    new DynaProperty("lastName",  String.class)
      };
 //建立動態類來設定動態屬性值
    BasicDynaClass dynaClass = new BasicDynaClass("employee", null, props);
    DynaBean employee = dynaClass.newInstance();
    employee.set("address", new HashMap());
    employee.set("subordinate", new mypackage.Employee[0]);
    employee.set("firstName", "Fred");
    employee.set("lastName", "Flintstone");

//也可以同上一樣 提供統一 的Get Set 還是字元操作 ^o^  哈哈
         BeanUtils.setProperty(bean,"address",new HashMap());
             BeanUtils.setProperty(bean,"address(ads1)","江西");
         BeanUtils.setProperty(bean,"name","liu");
         BeanUtils.setProperty(bean,"subordinate",new String[3]);
             BeanUtils.setProperty(bean,"subordinate[1]","heha");
         System.out.println(  BeanUtils.getProperty(bean,"address(ads1)") );
         System.out.println(  BeanUtils.getProperty(bean,"name") );
         System.out.println(  BeanUtils.getProperty(bean,"subordinate[1]") );

3)JDBC 擴充 Connection conn = ;
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery
    ("select accountid, name from customers");
    Iterator rows = (new ResultSetDynaClass(rs)).iterator();  //ResultSetDynaClass(java.sql.ResultSet resultSet)
    while (rows.hasNext()) {
 //利用動態bean進行輸出
    DynaBean row = (DynaBean) rows.next();   //連實體Bean都可以不要了 
        BeanUtils.copyProperties( MyBean ,row );  //  bean<--dynaBean 就這樣去得到值 ^o^ 
          MyBean.getAccountid();  ....           
    }
    rs.close();
    stmt.close();

4)HttpServletRequest 擴充    HttpServletRequest request = ;
    MyBean bean = ;
    HashMap map = new HashMap();
    Enumeration names = request.getParameterNames();
    while (names.hasMoreElements()) {
      String name = (String) names.nextElement();
      map.put(name, request.getParameterValues(name));
    }
    BeanUtils.populate(bean, map);//bean<--map  struts好象是就用這個 Form

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.