beanUtiles强大功能攻略_第1页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、beanutiles强大功能攻略1.属性的动态getter,ter 在这框架满天飞的年月,不能事事都保证执行getter,setter函数了,有时候属性是要需要按照名字动态取得的,就像这样: beanuti.getproperty(mybean,"code");而beanutils更强的功能是挺直拜访内嵌对象的属性,只要用法点号分隔。beanutils.getproperty(orderbean, "aress.city");相比之下其他类库的beanutils通常都很容易,不能拜访内嵌的对象,所以常常要用commons beanutils替换它们。be

2、anutils还支持list和map类型的属性。如下面的语法即可取得顾客列表中第一个顾客的名字beanutils.getproperty(orderbean, "");其中beanutils会用法convertutils类把字符串转为bean属性的真正类型,便利从httpservletrequest等对象中提取bean,或者把bean输出到页面。而propertyutils就会原色的保留bean本来的类型。2.beancompartor 动态排序 还是通过反射,动态设定bean根据哪个属性来排序,而不再需要在bean的compare接口举行复杂

3、的条件推断。 list peoples = .; / person对象的列表collections.(peoples, new beancomparator("age");假如要支持多个属性的复合排序,如"order by lastname,firstname" arraylist sortfields = new arraylist();sortfields.add(new beancomparator("lastname");sortfields.add(new beancomparator("firstname&quo

4、t;);comparatorchain multisort = new comparatorchain(sortfields);collections.sort(rows,multisort);其中comparatorchain属于jakata commons-lections包。假如age属性不是一般类型,构造函数需要再传入一个comparator对象为age变量排序。另外, beancompartor本身的compareblecomparator, 碰到属性为null就会抛出异样, 也不能设定升序还是降序。这个时候又要借助commons-collections包的comparatoruti

5、ls. comparator my = comparablecomparator.getinstae(); mycmp = comparatorutils.nulllowcomparator(mycmp); /允许null mycmp = comparatorutils.reverscomparator(mycmp); /逆序 comparator cmp = new beancomparator(sortcolumn, mycmp);3.converter 把request或reltset中的字符串绑定到对象的属性 常常要从request,resultset等对象取出值来赋入bean中,下面

6、的代码谁都写腻了,假如不用mvc框架的绑定功能的话。 sing 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.ntelement(); map.put(name, reques

7、t.getparametervalues(name); beanutils.populate(bean, map); 其中beanutils的populate办法或者getproperty,setproperty办法其实都会调用convert举行转换。 但converter只支持一些基本的类型,甚至连java.util.date类型也不支持。而且它比较笨的一个地方是当碰到不熟悉的类型时,竟然会抛出异样来。 对于date类型,我参考它的sql类型实现了一个converter,而且添加了一个设置日期格式的函数。要把这个converter注册,需要如下语句: convertutilsbean con

8、vertutils = new convertutilsbean(); dateconverter dateconverter = new dateconverter(); convertutils.register(dateconverter,date.class);/由于要注册converter,所以不能再用法beanutils的静态办法了,必需创建beanutilsbean实例beanutilsbean beanutils = new beanutilsbean(convertutils,new propertyutilsbean();beanutils.setproperty(bean

9、, name, value);4 其他功能 4.1 propertyutils,当属性为collection,map时的动态读取: collection: 提供index beanutils.getindexedproperty(orderbean,"items",1);或者 beanutils.getindexedproperty(orderbean,"items1");map: 提供key value beanutils.getmappedproperty(orderbean, "items","111");/

10、key-value goods_no=111 或者 beanutils.getmappedproperty(orderbean, "items(111)") 4.2 propertyutils,猎取属性的class类型 public ic 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减除不须要的vo和fobean http:/commons.a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论