惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
H
Help Net Security
B
Blog RSS Feed
G
Google Developers Blog
博客园 - 司徒正美
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
P
Proofpoint News Feed
小众软件
小众软件
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
V
V2EX
月光博客
月光博客
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
Arctic Wolf
Help Net Security
Help Net Security
Schneier on Security
Schneier on Security
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Palo Alto Networks Blog
T
Tenable Blog
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
F
Fortinet All Blogs
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Y
Y Combinator Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
NISL@THU
NISL@THU
GbyAI
GbyAI
博客园 - Franky
S
Secure Thoughts
有赞技术团队
有赞技术团队
PCI Perspectives
PCI Perspectives
U
Unit 42

博客园 - 张谊

[转]ubuntu下 手动安装 LAMP 和 JAVA环境 [转]ViewState的使用 [转]Working with user roles and permissions in SharePoint Object Model [原]SharePoint文档库上传文档 [原]SharePoint列表与文档库EventHandeler [转]ASP.NET缓存概念及其应用浅析 window.showModalDialog以及window.open用法简介 [原]欢迎加入QQ群 [原]如何在Silverlight中使用WebService绑定DataGrid [转]Silverlight中调用远程Web Service的权限问题 [转]BeanUtils接口和类 [转]Apache+Tomcat负载均衡及Session绑定的实现 [原]基于Caché多维数据库的SSH实现 [原]关于支付宝API开发的一点心得 [原]Oracle中列自增的方法 [原]Java反射示例 [原]JavaSocket实现广播聊天室 [转]翻译 一些很酷的.Net技巧 《生命如一泓清水》
[原]Commons- BeanUtils学习笔记
张谊 · 2009-04-22 · via 博客园 - 张谊

Jakarta-Commons- BeanUtils学习笔记:

Author:Tony.zhang  Date:April 22, 2009 Mail:setpsw@gmail.com

1.什么是BeanUtils

BeanUtils主要提供了对于JavaBean进行各种操作。

2.BeanUtils的作用:

在一般的写bean组件的时候,都必须要写settergetter方法,当然假如我们事先已经知道bean的相关属性和方法,写bean是比较简单的,但是组件太多的时候,重复编写经常是枯燥乏味令人厌烦的。但当有些时候我么需要调用动态对象的属性的时候,我们应该怎么来设定和获取对象的属性呢?BeanUtils就可以帮助我们来解决这个问题。它需要Jakarta-Commons -Collections包和Jakarta-Commons -logging包的支持。

3. org.apache.commons.beanutils

这个包主要提供用于操作JavaBean的工具类,Jakarta-Common-BeanUtils的主要功能都在这个包里实现。

BeanUtils可以直接getset一个属性的值。它将property分成3种类型:

       1Simple——简单类型,如StirngInt……

  (对于Simple类型,第二个参数直接是属性名即可,详见代码)

       2Indexed——索引类型,如 数组、arrayList……

      对于Indexed,则为“属性名[索引值]”,注意这里对于ArrayList和数组都可以用一样的方式进行操作,详见代码)

       3Maped——这个不用说也该知道,就是指Map,比如HashMap……

       对于Map类型,则需要以“属性名(key值)”的形式,详见代码)

    访问不同类型的数据可以直接调用函数getPropertysetProperty。它们都只有2个参数,第一个是JavaBean对象,第二个是要操作的属性名。

4.Converter RequestResultSet中的字符串绑定到对象的属性
  
经常要从request,resultSet等对象取出值来赋入bean中,如果不用MVC框架的绑定功能的话,下面的代码谁都写腻了。
String a = request.getParameter("a");   bean.setA(a);  

String b = request.getParameter("b");  bean.setB(b);

不妨写一个Binder自动绑定所有属性:


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);

 其中BeanUtilspopulate方法(Struts用于将一个map的值填充到一个bean)或者getProperty,setProperty方法其实都会调用convert进行转换。
    
Converter只支持一些基本的类型,甚至连java.util.Date类型也不支持。而且它比较笨的一个地方是当遇到不认识的类型时,居然会抛出异常来。 对于Date类型,我参考它的sqldate类型实现了一个Converter,而且添加了一个设置日期格式的函数。
要把这个Converter注册,需要如下语句:

//因为要注册converter,所以不能再使用BeanUtils的静态方法了,必须创建BeanUtilsBean实例:

BeanUtilsBean beanUtils =new BeanUtilsBean( convertUtils, new PropertyUtilsBean() ) ;

        beanUtils.setProperty( bean, name, value ) ;

5.BeanUtilsStruts中的应用:

下面的代码是我们经常在Struts中为了获得浏览器带来的参数的方法:

DynaActionForm daf = (DynaActionForm)form ;

        User user 

= new User() ;

        user.setId( daf.getString( 

"id" ) ) ;

        user.setPassword( daf.getString( 

"password" ) ) ;

        user.setUsername( daf.getString( 

"password" ) ) ;

是不是觉得每次写这些都是在重复造轮子呢?

在看看用BeanUtils写的:

J

BeanUtils.copyProperties( user, daf ) ;

就是这么简单

6.实例

public class Test {private String name; private HashMap address = new HashMap(); private String[] otherInfo; private ArrayList product; private ArrayList employee; private HashMap telephone;

省略get

/set

}

MainClass:

Jakarta-Commons- BeanUtils1.8.jar+JDK1.5

package com.cache.domain ;import java.util.HashMap ;import org.apache.commons.beanutils.BeanUtils ;/**

 * @Author:Tony.Zhang setpsw@gmail.com Apr 22, 2009 1:34:16 PM

*/public class MainClass {/**

     * 

@param args

     * 

@throws Exception*/public static void main( String[] args ) throws Exception{

        Test test 

= new Test();

        test.setName( 

"zhangyi" );// BeanUtils.getProperty( bean, name )// 动态取得

        System.out.println(BeanUtils.getProperty( test, 
"name" ));

        HashMap map 

= new HashMap();

        map.put( 

"1""13880808080" );

        map.put( 

"2""13550505050" );// BeanUtils.setProperty( bean, name, value )

        BeanUtils.setProperty( test, 
"telephone", map );// 动态取得

        System.out.println(BeanUtils.getProperty( test, 
"telephone(1)" ));/**

         * 直接进行Bean之间的clone 复制后的2个Bean的同一个属性可能拥有同一个对象的ref,

         * 这个在使用时要小心,特别是对于属性为类的情况。

*/

        Test test2 

= new Test();

        BeanUtils.copyProperties( test2, test );

        System.out.println(BeanUtils.getProperty( test2, 

"name" ));

    }

}

注:以上实例使用

其余的功能大家继续挖掘吧,可以参考官方API的说明J

ConvertUtilsBean convertUtils = new ConvertUtilsBean();

        DateConverter dateConverter 

= new DateConverter();

        convertUtils.register(dateConverter,Date.

class);