seasun  
          在不斷模仿、思考、總結(jié)中一步一步進步!
          公告
          •     我的blog中的部分資源是來自于網(wǎng)絡(luò)上,如果您認為侵犯了您的權(quán)利,請及時聯(lián)系我,我會盡快刪除!E-MAIL:shiwenfeng@aliyun.com和QQ:281340916,歡迎交流。

          日歷
          <2015年11月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          常用鏈接

          隨筆分類

          good blog author

          積分與排名

          • 積分 - 81738
          • 排名 - 700

          最新評論

          閱讀排行榜

           

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

          1.屬性的動態(tài)getter,setter

          在這框架滿天飛的年代,不能事事都保證執(zhí)行g(shù)etter,setter函數(shù)了,有時候?qū)傩允且枰鶕?jù)名字動態(tài)取得的,就像這樣:  
          BeanUtils.getProperty(myBean,"code");
          而BeanUtils更強的功能是直接訪問內(nèi)嵌對象的屬性,只要使用點號分隔。
          BeanUtils.getProperty(orderBean, "address.city");
          相比之下其他類庫的BeanUtils通常都很簡單,不能訪問內(nèi)嵌的對象,所以經(jīng)常要用Commons BeanUtils替換它們。
          BeanUtils還支持List和Map類型的屬性。如下面的語法即可取得顧客列表中第一個顧客的名字
          BeanUtils.getProperty(orderBean, "customers[1].name");
          其中BeanUtils會使用ConvertUtils類把字符串轉(zhuǎn)為Bean屬性的真正類型,方便從HttpServletRequest等對象中提取bean,或者把bean輸出到頁面。
          而PropertyUtils就會原色的保留Bean原來的類型。

          2.beanCompartor 動態(tài)排序

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

          如果要支持多個屬性的復(fù)合排序,如"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屬性不是普通類型,構(gòu)造函數(shù)需要再傳入一個comparator對象為age變量排序。
          另外, BeanCompartor本身的ComparebleComparator, 遇到屬性為null就會拋出異常, 也不能設(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等對象取出值來賦入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方法其實都會調(diào)用convert進行轉(zhuǎn)換。
              但Converter只支持一些基本的類型,甚至連java.util.Date類型也不支持。而且它比較笨的一個地方是當遇到不認識的類型時,居然會拋出異常來。
              對于Date類型,我參考它的sqldate類型實現(xiàn)了一個Converter,而且添加了一個設(shè)置日期格式的函數(shù)。
          要把這個Converter注冊,需要如下語句:

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



          //因為要注冊converter,所以不能再使用BeanUtils的靜態(tài)方法了,必須創(chuàng)建BeanUtilsBean實例
          BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());
          beanUtils.setProperty(bean, name, value);
          4 其他功能
          4.1 PropertyUtils,當屬性為Collection,Map時的動態(tài)讀?。?/span>
           
          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,動態(tài)創(chuàng)建對象
                public static Object invokeConstructor(Class klass, Object arg)
          4.4 MethodUtils,動態(tài)調(diào)用方法
              MethodUtils.invokeMethod(bean, methodName, parameter);
          4.5 動態(tài)Bean 用DynaBean減除不必要的VO和FormBean 
          posted on 2009-12-30 11:24 shiwf 閱讀(43726) 評論(4)  編輯  收藏 所屬分類: 3.6 BeanUtils
          評論:
          • # re: Beanutils基本用法  re: Beanutils基本用法 Posted @ 2013-01-11 16:26
            re: Beanutils基本用法  回復(fù)  更多評論   

          • # 求解BeanUtils的求解.populate(arg0, arg1)方法  毛耿寧 Posted @ 2015-05-19 19:30
            public class InsertServlet extends HttpServlet {
            private usersdao ud = new usersdao();

            public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {

            usersentity ue = new usersentity();

            try {
            BeanUtils.populate(ue, request.getParameterMap());
            ud.insertusers(ue);
            System.out.println("aaa");
            } catch (IllegalAccessException e) {
            e.printStackTrace();
            } catch (InvocationTargetException e) {
            e.printStackTrace();
            }
            }


            public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            doPost(request, response);
            }


            }
              回復(fù)  更多評論   

          • # re: Beanutils基本用法  諤諤 Posted @ 2015-11-18 10:13
            威威威威  回復(fù)  更多評論   

          • # re: Beanutils基本用法[未登錄]  123 Posted @ 2016-04-17 18:27
            1231231231312  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © shiwf Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 镇安县| 百色市| 康保县| 东丽区| 安龙县| 太保市| 郑州市| 木兰县| 洪江市| 阜新市| 威信县| 广丰县| 合川市| 兴义市| 大渡口区| 手机| 博湖县| 邳州市| 溆浦县| 宁波市| 崇阳县| 建始县| 茶陵县| 昭平县| 太仓市| 康平县| 沙雅县| 马关县| 五莲县| 静安区| 黑龙江省| 阿克陶县| 丘北县| 额尔古纳市| 酉阳| 治多县| 宁远县| 清水河县| 舒城县| 赫章县| 视频|