欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
始終會(huì )用上的Common BeanUtils


Beanutils用了魔術(shù)般的反射技術(shù),實(shí)現了很多夸張有用的功能,都是C/C++時(shí)代不敢想的。無(wú)論誰(shuí)的項目,始終一天都會(huì )用得上它。我算是后知后覺(jué)了,第一回看到它的時(shí)候居然錯過(guò)。

1.屬性的動(dòng)態(tài)getter、setter

在這框架滿(mǎn)天飛的年代,不能事事都保證執行g(shù)etter,setter函數了,有時(shí)候屬性是要根據名字動(dòng)態(tài)取得的,就像這樣:  
BeanUtils.getProperty(myBean,"code");
而Common BeanUtils的更強功能在于可以直接訪(fǎng)問(wèn)內嵌對象的屬性,只要使用點(diǎn)號分隔。
BeanUtils.getProperty(orderBean, "address.city");
相比之下其他類(lèi)庫的BeanUtils通常都很簡(jiǎn)單,不能訪(fǎng)問(wèn)內嵌的對象,所以有時(shí)要用Commons BeanUtils來(lái)替換它們。

BeanUtils還支持List和Map類(lèi)型的屬性,如下面的語(yǔ)法即可取得Order的顧客列表中第一個(gè)顧客的名字
BeanUtils.getProperty(orderBean, "customers[1].name");
其中BeanUtils會(huì )使用ConvertUtils類(lèi)把字符串轉為Bean屬性的真正類(lèi)型,方便從HttpServletRequest等對象中提取bean,或者把bean輸出到頁(yè)面。
而PropertyUtils就會(huì )原色的保留Bean原來(lái)的類(lèi)型。

2.BeanCompartor 動(dòng)態(tài)排序

還是通過(guò)反射,動(dòng)態(tài)設定Bean按照哪個(gè)屬性來(lái)排序,而不再需要在實(shí)現bean的Compare接口進(jìn)行復雜的條件判斷。
List peoples = ...; // Person對象的列表Collections.sort(peoples, new BeanComparator("age"));

如果要支持多個(gè)屬性的復合排序,如"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屬性不是普通類(lèi)型,構造函數需要再傳入一個(gè)comparator對象為age變量排序。
另外, BeanCompartor本身的ComparebleComparator, 遇到屬性為null就會(huì )拋出異常, 也不能設定升序還是降序。這個(gè)時(shí)候又要借助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中的字符串綁定到對象的屬性

   經(jīng)常要從request,resultSet等對象取出值來(lái)賦入bean中,如果不用MVC框架的綁定功能的話(huà),下面的代碼誰(shuí)都寫(xiě)膩了。

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

不妨寫(xiě)一個(gè)Binder自動(dòng)綁定所有屬性:

    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方法其實(shí)都會(huì )調用convert進(jìn)行轉換。
    但Converter只支持一些基本的類(lèi)型,甚至連java.util.Date類(lèi)型也不支持。而且它比較笨的一個(gè)地方是當遇到不認識的類(lèi)型時(shí),居然會(huì )拋出異常來(lái)。 對于Date類(lèi)型,我參考它的sqldate類(lèi)型實(shí)現了一個(gè)Converter,而且添加了一個(gè)設置日期格式的函數。
要把這個(gè)Converter注冊,需要如下語(yǔ)句:

    ConvertUtilsBean convertUtils = new ConvertUtilsBean();
   DateConverter dateConverter = new DateConverter();
   convertUtils.register(dateConverter,Date.class);



//因為要注冊converter,所以不能再使用BeanUtils的靜態(tài)方法了,必須創(chuàng )建BeanUtilsBean實(shí)例
BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());
beanUtils.setProperty(bean, name, value);
4 其他功能
4.1 ConstructorUtils,動(dòng)態(tài)創(chuàng )建對象
     public static Object invokeConstructor(Class klass, Object arg)
4.2 MethodUtils,動(dòng)態(tài)調用方法
    MethodUtils.invokeMethod(bean, methodName, parameter);

4.3 PropertyUtils,當屬性為Collection,Map時(shí)的動(dòng)態(tài)讀?。?br>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.4 PropertyUtils,直接獲取屬性的Class類(lèi)型
     public static Class getPropertyType(Object bean, String name)
4.5 動(dòng)態(tài)Bean 見(jiàn)用DynaBean減除不必要的VO和FormBean 


本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
commons-BeanUtils簡(jiǎn)介
[Jakarta Commons筆記](méi) Commons BeanUtils
Jakarta Commons:巧用類(lèi)和組件
commons-beanutils.jar中的DynaBean
apache commons beanutils
Apache Common BeanUtils
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久