Apache Beanutils基本用法
来源:互联网 发布:云计算实验室建设方案 编辑:程序博客网 时间:2024/05/17 09:09
Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的。无论谁的项目,始终一天都会用得上它。我算是后知后觉了,第一回看到它的时候居然错过。
1.属性的动态getter,setter
BeanUtils.getProperty(myBean,"code");
BeanUtils.getProperty(orderBean, "address.city");
BeanUtils还支持List和Map类型的属性。如下面的语法即可取得顾客列表中第一个顾客的名字
BeanUtils.getProperty(orderBean, "customers[1].name");
2.beanCompartor 动态排序
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);
经常要从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的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 其他功能或者
BeanUtils.getIndexedProperty(orderBean,"items[1]");
Map: 提供Key Value
BeanUtils.getMappedProperty(orderBean, "items","111");//key-value goods_no=111
或者
BeanUtils.getMappedProperty(orderBean, "items(111)")
public static Class getPropertyType(Object bean, String name)
public static Object invokeConstructor(Class klass, Object arg)
4.4 MethodUtils,动态调用方法
MethodUtils.invokeMethod(bean, methodName, parameter);
4.5 动态Bean 见用DynaBean减除不必要的VO和FormBean
备注:原贴链接
http://www.blogjava.net/shiwenfeng/archive/2009/12/30/307731.html
- Apache Beanutils基本用法
- Beanutils基本用法
- Beanutils基本用法
- Beanutils基本用法
- Beanutils基本用法
- Beanutils基本用法
- Beanutils的基本用法
- Beanutils基本用法
- Beanutils基本用法[转载]
- Beanutils基本用法
- Beanutils基本用法
- org.apache.commons.beanutils.BeanUtils用法
- Commons BeanUtils包基本用法
- org.apache.commons.beanutils.BeanUtils包的具体用法
- com.sun.org.apache.commons.beanutils.BeanUtils的用法简介
- Spring 中的BeanUtils与apache中的BeanUtils用法与比较
- Apache开源组件BeanUtils用法总结
- beanUtils基本用法——DynaBean
- LIST CONTROL
- 为什么写技术博客对新人如此重要?
- WM算法--多模式匹配
- Flex中ToggleButtonBar控件设置button的样式
- TCP/IP通信程序设计的丰富多样性
- Apache Beanutils基本用法
- jquery UI框架界面演试文档
- 【OpenCV】cv::Mat与IplImage类型转换
- Iframe正在加载时显示遮罩层
- SqlServer 系统静态函数
- ci service 和catalog.wci是什么?
- JavaScript prototype, 获取本机信息.
- js获取相对路径的方法
- linux-2.6.35内核配置和网卡驱动添加 .