beanutils的基本用法

2012/05/05 1988点热度 0人点赞 0条评论

1.属性的动态getter,setter

  在这框架满天飞的年代,不能事事都保证执行getter,setter函数了,有时候属性是要需要根据名字动态取得的,就像这样:  

BeanUtils.getProperty(myBean,"code");

  而BeanUtils更强的功能是直接访问内嵌对象的属性,只要使用点号分隔。

BeanUtils.getProperty(orderBean, "address.city");

 相比之下其他类库的BeanUtils通常都很简单,不能访问内嵌的对象,所以经常要用Commons BeanUtils替换它们。
 BeanUtils还支持List和Map类型的属性。如下面的语法即可取得顾客列表中第一个顾客的名字

BeanUtils.getProperty(orderBean, "customers[1].name");

 其中BeanUtils会使用ConvertUtils类把字符串转为Bean属性的真正类型,方便从HttpServletRequest等对象中提取bean,或者把bean输出到页面。

 而PropertyUtils就会原色的保留Bean原来的类型。

2.beanCompartor 动态排序

  还是通过反射,动态设定Bean按照哪个属性来排序,而不再需要在bean的Compare接口进行复杂的条件判断。

List peoples = ...; // Person对象的列表
Collections.sort(peoples, new BeanComparator("age"));

 如果要支持多个属性的复合排序,如"Order By lastName,firstName"

ArrayList sortFields = new ArrayList();
sortFields.add(new BeanComparator("lastName"));
sortFields.add(new BeanComparator("firstName"));
ComparatorChain multiSort = new ComparatorChain(sortFields);
Collections.sort(rows,multiSort);

  其中ComparatorChain属于jakata commons-collections包。
  如果age属性不是普通类型,构造函数需要再传入一个comparator对象为age变量排序。
  另外, BeanCompartor本身的ComparebleComparator, 遇到属性为null就会抛出异常, 也不能设定升序还是降序。
这个时候又要借助commons-collections包的ComparatorUtils.

Comparator mycmp = ComparableComparator.getInstance();
mycmp = ComparatorUtils.nullLowComparator(mycmp);  //允许null
mycmp = ComparatorUtils.reversedComparator(mycmp); //逆序
Comparator cmp = new BeanComparator(sortColumn, mycmp);

3.Converter 把Request或ResultSet中的字符串绑定到对象的属性

  经常要从request,resultSet等对象取出值来赋入bean中,下面的代码谁都写腻了,如果不用MVC框架的绑定功能的话。

  String a = request.getParameter("a");

  bean.setA(a);   String 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);

 一个利用beanutils将表单中的name属性对应的值封装到bean里面的方法

// 将 任意表单的请求参数 封装到指定的javabean中
    public static <T> T request2Bean(HttpServletRequest request, Class<T> beanClass) {
        try {
            // 实例化bean,当前的bean中必须有一个无参的构造方法
            T bean = beanClass.newInstance();
            // 获得所有的参数名
            Enumeration names = request.getParameterNames();
            while (names.hasMoreElements()) {
                String name = (String) names.nextElement();
                // 根据name获得参数值
                String value = request.getParameter(name);
                // 封装bean
                BeanUtils.setProperty(bean, name, value);
            }
            return bean;
        } catch (Exception e) {
            throw new WebUtilException(e);
        }
    }

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

ConvertUtilsBean convertUtils = new ConvertUtilsBean();
DateConverter dateConverter = new DateConverter();
convertUtils.register(dateConverter,Date.class);
//因为要注册converter,所以不能再使用BeanUtils的静态方法了,必须创建BeanUtilsBean实例
BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());
beanUtils.setProperty(bean, name, value);

4 其他功能

  4.1 PropertyUtils,当属性为Collection,Map时的动态读取:

    Collection: 提供index

BeanUtils.getIndexedProperty(orderBean,"items",1);
//或者
BeanUtils.getIndexedProperty(orderBean,"items[1]");

   Map: 提供Key Value

BeanUtils.getMappedProperty(orderBean, "items","111");//key-value goods_no=111
//或者
BeanUtils.getMappedProperty(orderBean, "items(111)")

4.2 PropertyUtils,获取属性的Class类型

public static Class getPropertyType(Object bean, String name)

 4.3 ConstructorUtils,动态创建对象

public static Object invokeConstructor(Class klass, Object arg)

 4.4 MethodUtils,动态调用方法

MethodUtils.invokeMethod(bean, methodName, parameter);

 4.5 动态Bean

DynaBean是BeanUtils里的宝贝之一。有了动态Bean,就不用写那么多VO出来了,特别那些只是为了和View交互而存在的bean,i hate Form Bean。
    DynaBean的用法很简单,特别是有了LazyBynaBean之后.

DynaBean car = new LazyDynaBean();
car.set("carNo",1);
car.set("owner","张三");
out.print(car.get("carNo"));

      看着像一个map,只不过多了反射的功能,所以支持那些使用反射来获取属性的场合。

      但是因为他没有car.getCarNo()这样的函数,只能用car.get("carNo"),所以也就不支持JSTL里面<c: out value="{car.carNo}">这样的语法,因为JSTL是默认转回car.getCarNo()的。
而且几经讨论,JSTL不支持 car.userFunction(),理由是不希望代码里有java代码:(
       因此,还是写<%=car.get("carNo")>好了,如果是陷在JSTL的一个循环结构里,就要用从pageContext里面把bean拿出来,
       又或者自己写的tag,幸亏也很简单,用BeanUtils.getProperty(bean,property)函数就可以了,
       又或者,用Velocity和Freemarker。

      最后,如果car.set("carNo",null),会得到一个莫名其妙的Object对象,如果要使它为Null,需要先定义他的类型。
      因此,对于可能为Null的列,需要用LazyClass定义类型,幸亏也很Lazy, 不可能为Null的值可以不管。

LazyDynaClass lc = new LazyDynaClass();
lc.add("owner",String.class); // 指定null converter String
DynaBean car = new LazyDynaBean(lc);
car.set("carNo",1);
car.set("owner",null);

yxkong

这个人很懒,什么都没留下

文章评论