BeanUtils工具
生活随笔
收集整理的這篇文章主要介紹了
BeanUtils工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.1 簡介
程序中對javabean的操作很頻繁, 所以apache提供了一套開源的api,方便對javabean的操作!即BeanUtils組件。
BeanUtils組件, 作用是簡化javabean的操作!
用戶可以從www.apache.org下載BeanUtils組件,然后再在項目中引入jar文件!
使用BenUtils組件:
- 1. 引入commons-beanutils-1.8.3.jar核心包
- 引入日志支持包: commons-logging-1.1.3.jar
如果缺少日志jar文件,報錯:
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 實例, 基本用法
方法1: 對象屬性的拷貝
BeanUtils.copyProperty(admin, "userName", "jack"); BeanUtils.setProperty(admin, "age", 18);方法2: 對象的拷貝
BeanUtils.copyProperties(newAdmin, admin);方法3: map數據拷貝到javabean中
【注意:map中的key要與javabean的屬性名稱一致】
例子
//1. 對javabean的基本操作@Testpublic void test1() throws Exception {// a. 基本操作Admin admin = new Admin(); // admin.setUserName("Jack"); // admin.setPwd("999");// b. BeanUtils組件實現對象屬性的拷貝BeanUtils.copyProperty(admin, "userName", "jack");BeanUtils.setProperty(admin, "age", 18);// 總結1: 對于基本數據類型,會自動進行類型轉換!// c. 對象的拷貝Admin newAdmin = new Admin();BeanUtils.copyProperties(newAdmin, admin);// d. map數據,拷貝到對象中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);// 測試System.out.println(adminMap.getUserName());System.out.println(adminMap.getAge());}1.3 實例, 日期類型的拷貝
需要注冊日期類型轉換器,2種方式參見下面代碼:
public class App {//1. 對javabean的基本操作@Testpublic void test1() throws Exception {// a. 基本操作Admin admin = new Admin(); // admin.setUserName("Jack"); // admin.setPwd("999");// b. BeanUtils組件實現對象屬性的拷貝BeanUtils.copyProperty(admin, "userName", "jack");BeanUtils.setProperty(admin, "age", 18);// 總結1: 對于基本數據類型,會自動進行類型轉換!// c. 對象的拷貝Admin newAdmin = new Admin();BeanUtils.copyProperties(newAdmin, admin);// d. map數據,拷貝到對象中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);// 測試System.out.println(adminMap.getUserName());System.out.println(adminMap.getAge());}//2. 自定義日期類型轉換器@Testpublic void test2() throws Exception {// 模擬表單數據String name = "jack";String age = "20";String birth = " ";// 對象Admin admin = new Admin();// 注冊日期類型轉換器:1, 自定義的方式ConvertUtils.register(new Converter() {// 轉換的內部實現方法,需要重寫@Overridepublic Object convert(Class type, Object value) {// 判斷if (type != Date.class) {return null;}if (value == null || "".equals(value.toString().trim())) {return null;}try {// 字符串轉換為日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");return sdf.parse(value.toString());} catch (ParseException e) {throw new RuntimeException(e);}}},Date.class);// 把表單提交的數據,封裝到對象中BeanUtils.copyProperty(admin, "userName", name);BeanUtils.copyProperty(admin, "age", age);BeanUtils.copyProperty(admin, "birth", birth);//------ 測試------System.out.println(admin);}//2. 使用提供的日期類型轉換器工具類@Testpublic void test3() throws Exception {// 模擬表單數據String name = "jack";String age = "20";String birth = null;// 對象Admin admin = new Admin();// 注冊日期類型轉換器:2, 使用組件提供的轉換器工具類ConvertUtils.register(new DateLocaleConverter(), Date.class);// 把表單提交的數據,封裝到對象中BeanUtils.copyProperty(admin, "userName", name);BeanUtils.copyProperty(admin, "age", age);BeanUtils.copyProperty(admin, "birth", birth);//------ 測試------System.out.println(admin);} }1.4 應用
public class WebUtils {@Deprecatedpublic static <T> T copyToBean_old(HttpServletRequest request, Class<T> clazz) {try {// 創建對象T t = clazz.newInstance();// 獲取所有的表單元素的名稱Enumeration<String> enums = request.getParameterNames();// 遍歷while (enums.hasMoreElements()) {// 獲取表單元素的名稱:<input type="password" name="pwd"/>String name = enums.nextElement(); // pwd// 獲取名稱對應的值String value = request.getParameter(name);// 把指定屬性名稱對應的值進行拷貝BeanUtils.copyProperty(t, name, value);}return t;} catch (Exception e) {throw new RuntimeException(e);}}/*** 處理請求數據的封裝*/public static <T> T copyToBean(HttpServletRequest request, Class<T> clazz) {try {// (注冊日期類型轉換器)// 創建對象T t = clazz.newInstance();BeanUtils.populate(t, request.getParameterMap());return t;} catch (Exception e) {throw new RuntimeException(e);}} }總結
以上是生活随笔為你收集整理的BeanUtils工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IntelliJ IDEA安装与JDK
- 下一篇: 计算机命令指示符大全,常用CMD命令提示