`
jxxms
  • 浏览: 106989 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

commons.beanutils

阅读更多

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

2.已经使用BeanUtils的事例 *构建和java对象模型交互的脚本语言,比如Bean Scripting Framework *构建web层显示和相似用途的模板语言处理器,如jsp或者Velocity *构建适用于jsp和xsp环境的自定义标签库,如Jakarta Taglibs,Struts,Cocoon *处理基于xml的配置资源文件,如Ant创建脚本,应用程序部署文件和Tomcat的xml配置文件

3.API简介 BeanUtils 的 Java API 主要的 package 总共四个 org.apache.commons.beanutils 这个是BeanUtils最基本的包,提供了处理getter和setter方法属性的类 org.apache.commons.beanutils.converters Converter接口的标准实现,在启动的时候和ConvertUtils一同注册 org.apache.commons.beanutils.locale 组件的本地化处理包 org.apache.commons.beanutils.locale.converters LocaleConverter接口的标准实现,启动的时候和LocaleConvertUtils 一同注册 4.org.apache.commons.beanutils介绍这四个包本专题只介绍这一个,其他的使用的时候请自己参考它的文档,文档内容写得很好. 1.PropertyUtils 它支持三种类型的属性值-Simple,Indexed,Mapped 我们先创建一个简单的bean

public class Employee {

  public Address getAddress(String type);

  public void setAddress(String type, Address address);

  public Employee getSubordinate(int index);

  public void setSubordinate(int index, Employee subordinate);

  public String getFirstName();

  public void setFirstName(String firstName);

  public String getLastName();

  public void setLastName(String lastName); } *Simple属性访问方法  

  PropertyUtils.getSimpleProperty(Object bean, String name)  

  PropertyUtils.setSimpleProperty(Object bean, String name, Object value)

实现代码:

Employee employee = ...;

String firstName = (String) PropertyUtils.getSimpleProperty(employee, "firstName");

String lastName = (String) PropertyUtils.getSimpleProperty(employee, "lastName"); ... manipulate the values ... PropertyUtils.setSimpleProperty(employee, "firstName", firstName); PropertyUtils.setSimpleProperty(employee, "lastName", lastName); *Indexed属性访问方法

PropertyUtils.getIndexedProperty(Object bean, String name) PropertyUtils.getIndexedProperty(Object bean, String name, int index) PropertyUtils.setIndexedProperty(Object bean, String name, Object value)

PropertyUtils.setIndexedProperty(Object bean, String name, int index, Object value)

实现代码:

Employee employee = ...;

int index = ...;

String name = "subordinate[" + index + "]";

Employee subordinate = (Employee) PropertyUtils.getIndexedProperty(employee, name);

//根据value取得属性

Employee employee = ...;

 int index = ...;

Employee subordinate = (Employee) PropertyUtils.getIndexedProperty(employee, "subordinate", index);

//根据索引 值取的属性值 *Mapped属性访问方法 PropertyUtils.getMappedProperty(Object bean, String name) PropertyUtils.getMappedProperty(Object bean, String name, String key) PropertyUtils.setMappedProperty(Object bean, String name, Object value) PropertyUtils.setMappedProperty(Object bean, String name, String key, Object value)

实现代码:

 Employee employee = ...; Address address = ...; PropertyUtils.setMappedProperty(employee, "address(home)", address);

//根据数组

 //内的值来去的相应的属性值

Employee employee = ...;

 Address address = ...;

 PropertyUtils.setMappedProperty(employee, "address", "home", address); *Nested属性访问方法

//Nested的意思是参数中包含组件

PropertyUtils.getNestedProperty(Object bean, String name) PropertyUtils.setNestedProperty(Object bean, String name, Object value)

实现代码:

 String city = (String) PropertyUtils.getNestedProperty(employee, "address(home).city");

2.BeanUtils.DynaBean and BeanUtils.DynaClass 接口介绍 DynaBean必须有一个bean来实现这个接口,DynaClass必有一个bean的属性集来实现 *BasicDynaBean and BasicDynaClass - 基本的 Dynamic 类型 基本的API: BasicDynaClass(java.lang.String name, java.lang.Class dynaBeanClass, DynaProperty[] properties) BasicDynaBean(DynaClass dynaClass) 我们线定义一下基本的代码:

//定义动态属性集

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

*ResultSetDynaClass (Wraps ResultSet in DynaBeans) - 使用 ResultSet 的 Dynamic JavaBean API:

ResultSetDynaClass(java.sql.ResultSet resultSet) ResultSetDynaClass(java.sql.ResultSet resultSet, boolean lowerCase)

Connection conn = ...;

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery ("select account_id, name from customers");

 Iterator rows = (new ResultSetDynaClass(rs)).iterator();

while (rows.hasNext()) { //利用动态bean进行输出 DynaBean row = (DynaBean) rows.next();

 System.out.println("Account number is " + row.get("account_id") + " and name is " + row.get("name")); } rs.close();

 stmt.close();

*RowSetDynaClass (Disconnected ResultSet as DynaBeans) - 使用 RowSet 的 Dynamic JavaBean API: RowSetDynaClass(java.sql.ResultSet resultSet) RowSetDynaClass(java.sql.ResultSet resultSet, boolean lowerCase)
Connection conn = ...;

// 从缓冲池取得连接

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("SELECT ...");

RowSetDynaClass rsdc = new RowSetDynaClass(rs);

rs.close(); stmt.close(); ...;

//关闭连接返回缓冲池

List rows = rsdc.getRows(); ...;

// 处理得到的行

2.BeanUtils.ConvertUtils 介绍 这个包主要用来转换从request传递过来的函数 主要函数:

ConvertUtils().convert(java.lang.Object value)

ConvertUtils().convert(java.lang.String[] values, java.lang.Class clazz) ConvertUtils().convert(java.lang.String value, java.lang.Class clazz)

 实现例子:

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

//用ConvertUtils进行转换 目前支持的转换类型:

java.lang.BigDecimal java.lang.BigInteger boolean and java.lang.Boolean byte and java.lang.Byte char and java.lang.Character java.lang.Class double and java.lang.Double float and java.lang.Float int and java.lang.Integer long and java.lang.Long short and java.lang.Short java.lang.String java.sql.Date java.sql.Time java.sql.Timestamp

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics