Apache的BeanUtils组件学习

来源:互联网 发布:split赋值给数组 编辑:程序博客网 时间:2024/06/06 09:52

转自:  http://blog.csdn.net/qq_35448976/article/details/77816420


1、注意点

1.1、使用BeanUtils组件必须引入BeanUtils核心包,还需要引入日志支持包

1.2、BeanUtils的copyProperty与setProperty的实现原理-反射

1.3、对于基本基本数据类型,会自动进行类型转换

1.4、BeanUtils的populate拷贝map数据到对象中时,map中的key要与javabean的属 性名称一致

1.5、可以与servelt混用,request.getParameterMap()方法会的map集合,再用BeanUtils 的组件直接将map对象保存到javabean中

 

2、BeanUtils组件

2.1、简介

程序中对javabean的操作很频繁, 所以apache提供了一套开源的api,方便对javabean的操作!即BeanUtils组件。

2.2、BeanUtils组件作用

简化javabean的操作!

用户可以从www.apache.org下载BeanUtils组件,然后再在项目中引入jar文件!

 

 

2.3、使用BenUtils组件:

1. 引入commons-beanutils-1.8.3.jar核心包

2. 引入日志支持包: commons-logging-1.1.3.jar

 

2.4、如果缺少日志jar文件,报错:

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

at org.apache.commons.beanutils.ConvertUtilsBean.<init>(ConvertUtilsBean.java:157)

at org.apache.commons.beanutils.BeanUtilsBean.<init>(BeanUtilsBean.java:117)

at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:68)

at

 

3、实例, 基本用法

方法1: 对象属性的拷贝

BeanUtils.copyProperty(admin, "userName", "jack");

BeanUtils.setProperty(admin, "age", 18);

方法2: 对象的拷贝

BeanUtils.copyProperties(newAdmin, admin);

方法3: map数据拷贝到javabean中  

【注意:map中的key要与javabean的属性名称一致】

BeanUtils.populate(adminMap, map);


[java] view plain copy
 print?
  1. //1. 对javabean的基本操作  
  2. @Test  
  3. public void test1() throws Exception {  
  4. // a. 基本操作  
  5. Admin admin = new Admin();  
  6. //admin.setUserName("Jack");  
  7. //admin.setPwd("999");  
  8. // b. BeanUtils组件实现对象属性的拷贝  
  9. BeanUtils.copyProperty(admin, "userName""jack");  
  10. BeanUtils.setProperty(admin, "age"18);  
  11. // 总结1: 对于基本数据类型,会自动进行类型转换!  
  12. // c. 对象的拷贝  
  13. Admin newAdmin = new Admin();  
  14. BeanUtils.copyProperties(newAdmin, admin);  
  15. // d. map数据,拷贝到对象中  
  16. Admin adminMap = new Admin();  
  17. Map<String,Object> map = new HashMap<String,Object>();  
  18. map.put("userName""Jerry");  
  19. map.put("age"29);  
  20. // 注意:map中的key要与javabean的属性名称一致  
  21. BeanUtils.populate(adminMap, map);  
  22. // 测试  
  23. System.out.println(adminMap.getUserName());  
  24. System.out.println(adminMap.getAge());  
  25. }  

  

 

4、实例, 日期类型的拷贝

需要注册日期类型转换器,2种方式参见下面代码:

[java] view plain copy
 print?
  1. public class App {  
  2.    
  3. //1. 对javabean的基本操作  
  4. @Test  
  5. public void test1() throws Exception {  
  6. // a. 基本操作  
  7. Admin admin = new Admin();  
  8. //admin.setUserName("Jack");  
  9. //admin.setPwd("999");  
  10. // b. BeanUtils组件实现对象属性的拷贝  
  11. BeanUtils.copyProperty(admin, "userName""jack");  
  12. BeanUtils.setProperty(admin, "age"18);  
  13. // 总结1: 对于基本数据类型,会自动进行类型转换!  
  14. // c. 对象的拷贝  
  15. Admin newAdmin = new Admin();  
  16. BeanUtils.copyProperties(newAdmin, admin);  
  17. // d. map数据,拷贝到对象中  
  18. Admin adminMap = new Admin();  
  19. Map<String,Object> map = new HashMap<String,Object>();  
  20. map.put("userName""Jerry");  
  21. map.put("age"29);  
  22. // 注意:map中的key要与javabean的属性名称一致  
  23. BeanUtils.populate(adminMap, map);  
  24. // 测试  
  25. System.out.println(adminMap.getUserName());  
  26. System.out.println(adminMap.getAge());  
  27. }  
  28. //2. 自定义日期类型转换器  
  29. @Test  
  30. public void test2() throws Exception {  
  31. // 模拟表单数据  
  32. String name = "jack";  
  33. String age = "20";  
  34. String birth = "   ";  
  35. // 对象  
  36. Admin admin = new Admin();  
  37. // 注册日期类型转换器:1, 自定义的方式  
  38. ConvertUtils.register(new Converter() {  
  39. // 转换的内部实现方法,需要重写  
  40. @Override  
  41. public Object convert(Class type, Object value) {  
  42. // 判断  
  43. if (type != Date.class) {  
  44. return null;  
  45. }  
  46. if (value == null || "".equals(value.toString().trim())) {  
  47. return null;  
  48. }  
  49. try {  
  50. // 字符串转换为日期  
  51. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  52. return sdf.parse(value.toString());  
  53. catch (ParseException e) {  
  54. throw new RuntimeException(e);  
  55. }  
  56. }  
  57. },Date.class);  
  58. // 把表单提交的数据,封装到对象中  
  59. BeanUtils.copyProperty(admin, "userName", name);  
  60. BeanUtils.copyProperty(admin, "age", age);  
  61. BeanUtils.copyProperty(admin, "birth", birth);  
  62. //------ 测试------  
  63. System.out.println(admin);  
  64. }  
  65. //2. 使用提供的日期类型转换器工具类  
  66. @Test  
  67. public void test3() throws Exception {  
  68. // 模拟表单数据  
  69. String name = "jack";  
  70. String age = "20";  
  71. String birth = null;  
  72. // 对象  
  73. Admin admin = new Admin();  
  74. // 注册日期类型转换器:2, 使用组件提供的转换器工具类  
  75. ConvertUtils.register(new DateLocaleConverter(), Date.class);  
  76. // 把表单提交的数据,封装到对象中  
  77. BeanUtils.copyProperty(admin, "userName", name);  
  78. BeanUtils.copyProperty(admin, "age", age);  
  79. BeanUtils.copyProperty(admin, "birth", birth);  
  80. //------ 测试------  
  81. System.out.println(admin);  
  82. }  
  83. }  

  

5、应用

[java] view plain copy
 print?
  1. public class WebUtils {  
  2.    
  3. @Deprecated  
  4. public static <T> T copyToBean_old(HttpServletRequest request, Class<T> clazz) {  
  5. try {  
  6. // 创建对象  
  7. T t = clazz.newInstance();  
  8. // 获取所有的表单元素的名称  
  9. Enumeration<String> enums = request.getParameterNames();  
  10. // 遍历  
  11. while (enums.hasMoreElements()) {  
  12. // 获取表单元素的名称:<input type="password" name="pwd"/>  
  13. String name = enums.nextElement();  // pwd  
  14. // 获取名称对应的值  
  15. String value = request.getParameter(name);  
  16. // 把指定属性名称对应的值进行拷贝  
  17. BeanUtils.copyProperty(t, name, value);  
  18. }  
  19. return t;  
  20. catch (Exception e) {  
  21. throw new RuntimeException(e);  
  22. }  
  23. }  
  24. /** 
  25.  * 处理请求数据的封装 
  26.  */  
  27. public static <T> T copyToBean(HttpServletRequest request, Class<T> clazz) {  
  28. try {  
  29. // (注册日期类型转换器)  
  30. // 创建对象  
  31. T t = clazz.newInstance();  
  32. BeanUtils.populate(t, request.getParameterMap());  
  33. return t;  
  34. catch (Exception e) {  
  35. throw new RuntimeException(e);  
  36. }  
  37. }  
  38. }  
原创粉丝点击