posts - 165, comments - 198, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Jakarta Commons BeanUtils 学习

Posted on 2007-08-10 10:36 G_G 阅读(1260) 评论(0)  编辑  收藏 所属分类: Jakarta Commons
参考: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.get
Accountid();  ....           
    }
    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


只有注册用户登录后才能发表评论。


网站导航: