posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Jakarta Commons BeanUtils 學習

          Posted on 2007-08-10 10:36 G_G 閱讀(1267) 評論(0)  編輯  收藏 所屬分類: Jakarta Commons
          參考:http://www.duduwolf.com/wiki/2007/296.html
          ????? http://www.chinaitpower.com/A/2005-07-03/150232.html

          1)普通的Bean處理? 不管是什么Set參數為3個 ( 對象本身, 屬性名或屬性內位置, 值 ) //list的add有點特別
          ??? ??? ??? ??? ? ??? ??? ? Get
          參數為2個( 對象本身, 屬性名或屬性內位置 )
          ??
          ????????Employee?em?=?new?Employee();? //String name; String[] ss;? Map map; List list;
          ????????BeanUtils.setProperty(em,
          "name","liukaiyi");? //String set
          ????????BeanUtils.setProperty(em,
          "ss",new?String[]{"1","2","3"}); //String[] set
          ??????? ?? BeanUtils.setProperty(em,"ss[2]","google");? //String[2] set

          ????????BeanUtils.setProperty(em,
          "map",new?HashMap());?? //Map set?
          ????????????BeanUtils.setProperty(em,"map(key)","value");? //Map.put(Key,Value)
          ????????????
          ????????BeanUtils.setProperty(em,
          "list",new?ArrayList(?Arrays.asList(?new?Object[20]?)?)); //List有點特別
          ????????????BeanUtils.setProperty(em,"list[0]","list");?????? //不可以直接添加???
          ????????????
          ????????BeanUtils.setProperty(em,
          "avg","23");?????? //int set
          ????????????????
          ????????
          ????????System.out.println(?BeanUtils.getSimpleProperty(em,
          "name")?);???
          ????????System.out.println(?BeanUtils.getProperty(em,"ss[2]")?);
          ????????System.out.println(?BeanUtils.getProperty(em,"map(key)")?);
          ????????
          ????????System.out.println(?BeanUtils.getProperty(em,
          "list[0].class")?);? // 取的是 ==list.get(0).getClass()
          ????????
          ????????System.out.println(?BeanUtils.getProperty(em,
          "avg")?);?

          2)動態屬性
          ?//定義動態屬性集
          ?DynaProperty[]?props?=?new?DynaProperty[]{
          ????
          new?DynaProperty("address",?java.util.Map.class),
          ????
          new?DynaProperty("subordinate",?mypackage.Employee[].class),
          ????
          new?DynaProperty("firstName",?String.class),
          ????
          new?DynaProperty("lastName",??String.class)
          ??????};
          ?
          //創建動態類來設定動態屬性值
          ????BasicDynaClass?dynaClass?=?new?BasicDynaClass("employee",?null,?props);
          ??? DynaBean?employee?
          =?dynaClass.newInstance();
          ????employee.set(
          "address",?new?HashMap());
          ????employee.set(
          "subordinate",?new?mypackage.Employee[0]);
          ????employee.set(
          "firstName",?"Fred");
          ????employee.set(
          "lastName",?"Flintstone");

          //也可以同上一樣 提供統一 的Get Set 還是 字符操作 ^o^? 哈哈
          ??? ???? BeanUtils.setProperty(bean,"address",new HashMap());
          ??? ??? ???? BeanUtils.setProperty(bean,"address(ads1)","江西");
          ??? ??? ?BeanUtils.setProperty(bean,"name","liu");
          ??? ??? ?BeanUtils.setProperty(bean,"subordinate",new String[3]);
          ??? ??? ???? BeanUtils.setProperty(bean,"subordinate[1]","heha");
          ??? ??? ?System.out.println(? BeanUtils.getProperty(bean,"address(ads1)") );
          ??? ??? ?System.out.println(? BeanUtils.getProperty(bean,"name") );
          ??? ??? ?System.out.println(? BeanUtils.getProperty(bean,"subordinate[1]") );

          3)JDBC 擴展
          ?Connection?conn?=?;
          ????Statement?stmt?
          =?conn.createStatement();
          ????ResultSet?rs?
          =?stmt.executeQuery
          ????(
          "select?accountid,?name?from?customers");
          ????Iterator?rows?
          =?(new?ResultSetDynaClass(rs)).iterator();? //ResultSetDynaClass(java.sql.ResultSet resultSet)
          ????while?(rows.hasNext())?{
          ?
          //利用動態bean進行輸出
          ????DynaBean?row?=?(DynaBean)?rows.next();?? //連實體Bean都可以不要了?
          ??? ??? BeanUtils.copyProperties( MyBean ,row );? //? bean<--dynaBean 就這樣去得到值
          ^o^?
          ??? ?? ?? MyBean.get
          Accountid();? ....???????????
          ????}
          ????rs.close();
          ????stmt.close();

          4)HttpServletRequest 擴展
          ????HttpServletRequest?request?=?;
          ????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);
          //bean<--map? struts好象是就用這個 Form


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


          網站導航:
           
          主站蜘蛛池模板: 西青区| 巴青县| 棋牌| 奇台县| 铜陵市| 焦作市| 贵南县| 巴林左旗| 民县| 公主岭市| 溧水县| 昌图县| 庆城县| 桃源县| 兴和县| 丘北县| 赣州市| 芷江| 分宜县| 洛扎县| 满洲里市| 波密县| 游戏| 福安市| 克拉玛依市| 灌阳县| 屏东市| 东宁县| 海门市| 当阳市| 汝南县| 清流县| 西平县| 鄢陵县| 龙南县| 铜鼓县| 临汾市| 山西省| 云龙县| 宁化县| 泽州县|