BeanUtils工具
1.1 簡介
程序中對(duì)javabean的操作很頻繁, 所以apache提供了一套開源的api,方便對(duì)javabean的操作!即BeanUtils組件。
BeanUtils組件, 作用是簡化javabean的操作!
用戶可以從www.apache.org下載BeanUtils組件,然后再在項(xiàng)目中引入jar文件!
使用BenUtils組件:
- 1. 引入commons-beanutils-1.8.3.jar核心包
- 引入日志支持包: commons-logging-1.1.3.jar
如果缺少日志jar文件,報(bào)錯(cuò):
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.beanutils.ConvertUtilsBean.(ConvertUtilsBean.java:157)
at org.apache.commons.beanutils.BeanUtilsBean.(BeanUtilsBean.java:117)
at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:68)
at
1.2 實(shí)例, 基本用法
方法1: 對(duì)象屬性的拷貝
BeanUtils.copyProperty(admin, "userName", "jack"); BeanUtils.setProperty(admin, "age", 18);方法2: 對(duì)象的拷貝
BeanUtils.copyProperties(newAdmin, admin);方法3: map數(shù)據(jù)拷貝到j(luò)avabean中
【注意:map中的key要與javabean的屬性名稱一致】
例子
//1. 對(duì)javabean的基本操作@Testpublic void test1() throws Exception {// a. 基本操作Admin admin = new Admin(); // admin.setUserName("Jack"); // admin.setPwd("999");// b. BeanUtils組件實(shí)現(xiàn)對(duì)象屬性的拷貝BeanUtils.copyProperty(admin, "userName", "jack");BeanUtils.setProperty(admin, "age", 18);// 總結(jié)1: 對(duì)于基本數(shù)據(jù)類型,會(huì)自動(dòng)進(jìn)行類型轉(zhuǎn)換!// c. 對(duì)象的拷貝Admin newAdmin = new Admin();BeanUtils.copyProperties(newAdmin, admin);// d. map數(shù)據(jù),拷貝到對(duì)象中Admin adminMap = new Admin();Map<String,Object> map = new HashMap<String,Object>();map.put("userName", "Jerry");map.put("age", 29);// 注意:map中的key要與javabean的屬性名稱一致BeanUtils.populate(adminMap, map);// 測(cè)試System.out.println(adminMap.getUserName());System.out.println(adminMap.getAge());}1.3 實(shí)例, 日期類型的拷貝
需要注冊(cè)日期類型轉(zhuǎn)換器,2種方式參見下面代碼:
public class App {//1. 對(duì)javabean的基本操作@Testpublic void test1() throws Exception {// a. 基本操作Admin admin = new Admin(); // admin.setUserName("Jack"); // admin.setPwd("999");// b. BeanUtils組件實(shí)現(xiàn)對(duì)象屬性的拷貝BeanUtils.copyProperty(admin, "userName", "jack");BeanUtils.setProperty(admin, "age", 18);// 總結(jié)1: 對(duì)于基本數(shù)據(jù)類型,會(huì)自動(dòng)進(jìn)行類型轉(zhuǎn)換!// c. 對(duì)象的拷貝Admin newAdmin = new Admin();BeanUtils.copyProperties(newAdmin, admin);// d. map數(shù)據(jù),拷貝到對(duì)象中Admin adminMap = new Admin();Map<String,Object> map = new HashMap<String,Object>();map.put("userName", "Jerry");map.put("age", 29);// 注意:map中的key要與javabean的屬性名稱一致BeanUtils.populate(adminMap, map);// 測(cè)試System.out.println(adminMap.getUserName());System.out.println(adminMap.getAge());}//2. 自定義日期類型轉(zhuǎn)換器@Testpublic void test2() throws Exception {// 模擬表單數(shù)據(jù)String name = "jack";String age = "20";String birth = " ";// 對(duì)象Admin admin = new Admin();// 注冊(cè)日期類型轉(zhuǎn)換器:1, 自定義的方式ConvertUtils.register(new Converter() {// 轉(zhuǎn)換的內(nèi)部實(shí)現(xiàn)方法,需要重寫@Overridepublic Object convert(Class type, Object value) {// 判斷if (type != Date.class) {return null;}if (value == null || "".equals(value.toString().trim())) {return null;}try {// 字符串轉(zhuǎn)換為日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");return sdf.parse(value.toString());} catch (ParseException e) {throw new RuntimeException(e);}}},Date.class);// 把表單提交的數(shù)據(jù),封裝到對(duì)象中BeanUtils.copyProperty(admin, "userName", name);BeanUtils.copyProperty(admin, "age", age);BeanUtils.copyProperty(admin, "birth", birth);//------ 測(cè)試------System.out.println(admin);}//2. 使用提供的日期類型轉(zhuǎn)換器工具類@Testpublic void test3() throws Exception {// 模擬表單數(shù)據(jù)String name = "jack";String age = "20";String birth = null;// 對(duì)象Admin admin = new Admin();// 注冊(cè)日期類型轉(zhuǎn)換器:2, 使用組件提供的轉(zhuǎn)換器工具類ConvertUtils.register(new DateLocaleConverter(), Date.class);// 把表單提交的數(shù)據(jù),封裝到對(duì)象中BeanUtils.copyProperty(admin, "userName", name);BeanUtils.copyProperty(admin, "age", age);BeanUtils.copyProperty(admin, "birth", birth);//------ 測(cè)試------System.out.println(admin);} }1.4 應(yīng)用
public class WebUtils {@Deprecatedpublic static <T> T copyToBean_old(HttpServletRequest request, Class<T> clazz) {try {// 創(chuàng)建對(duì)象T t = clazz.newInstance();// 獲取所有的表單元素的名稱Enumeration<String> enums = request.getParameterNames();// 遍歷while (enums.hasMoreElements()) {// 獲取表單元素的名稱:<input type="password" name="pwd"/>String name = enums.nextElement(); // pwd// 獲取名稱對(duì)應(yīng)的值String value = request.getParameter(name);// 把指定屬性名稱對(duì)應(yīng)的值進(jìn)行拷貝BeanUtils.copyProperty(t, name, value);}return t;} catch (Exception e) {throw new RuntimeException(e);}}/*** 處理請(qǐng)求數(shù)據(jù)的封裝*/public static <T> T copyToBean(HttpServletRequest request, Class<T> clazz) {try {// (注冊(cè)日期類型轉(zhuǎn)換器)// 創(chuàng)建對(duì)象T t = clazz.newInstance();BeanUtils.populate(t, request.getParameterMap());return t;} catch (Exception e) {throw new RuntimeException(e);}} }總結(jié)
以上是生活随笔為你收集整理的BeanUtils工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IntelliJ IDEA安装与JDK
- 下一篇: 计算机在会计专业的作用论文开题报告,会计