beanUtils操作bean的属性

beanUtils操纵bean属性:

    需要jar包commons-beanutils-x.x.x.jar
    同时commons-beanutils-x.x.x.jar需要commons-logging.jar日志记录器来支持
    所以用beanUtils的时候需要导入两个jar包
例:

javaBean:

 1 import java.util.Date;

 2 

 3 /**

 4  * 

 5  * @功能:javaBean

 6  * 

 7  * @日期:2013-10-24

 8  * 

 9  */

10 public class Person {

11 

12     private String name;

13     private int age;

14     private Date birthday;

15     

16     public Date getBirthday() {

17         return birthday;

18     }

19 

20     public void setBirthday(Date birthday) {

21         this.birthday = birthday;

22     }

23     

24     public String getName() {

25         return name;

26     }

27     public void setName(String name) {

28         this.name = name;

29     }

30     public int getAge() {

31         return age;

32     }

33     public void setAge(int age) {

34         this.age = age;

35     }

36     

37 }

测试类:

  1 import java.lang.reflect.InvocationTargetException;

  2 import java.text.ParseException;

  3 import java.text.SimpleDateFormat;

  4 import java.util.Date;

  5 import java.util.HashMap;

  6 import java.util.Map;

  7 

  8 import org.apache.commons.beanutils.BeanUtils;

  9 import org.apache.commons.beanutils.ConversionException;

 10 import org.apache.commons.beanutils.ConvertUtils;

 11 import org.apache.commons.beanutils.Converter;

 12 import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

 13 import org.junit.Test;

 14 

 15 /**

 16  * 

 17  * @功能:BeanUtils

 18  * 

 19  * @日期:2013-10-24

 20  */

 21 public class BeanUtilsTest {

 22 

 23     @Test

 24     public void test1() throws IllegalAccessException, InvocationTargetException{

 25         Person p = new Person();

 26         

 27         String name = "张三";

 28         String age = "25";

 29         String date = "1988-01-04";

 30         /*

 31          * beanutils操纵bean的属性如下,

 32          * 第一参数传一个对象,第二个参数为所操作属性的名称,第三个参数为操作的属性的值

 33          * beanutils可以将属性类型自动转换成javaBean里的属性类型(只限于8种基本数据类型)

 34          * 想要将复杂类型的属性转换成其他型则需要注册一个转换器来实现

 35          * test2()为实现String转换为Date的测试过程

 36          */

 37         BeanUtils.setProperty(p, "name", name);

 38         BeanUtils.setProperty(p, "age", age);

 39         //如果将String类型的日期用如下方式来设置bean的属性,不会转换成功,会报出如下错误

 40         //BeanUtils.setProperty(p, "birthday", date);

 41         /*

 42          * 2013-10-24 14:48:34 org.apache.commons.beanutils.converters.DateConverter toDate

 43          *警告:     DateConverter does not support default String to 'Date' conversion.

 44          *2013-10-24 14:48:34 org.apache.commons.beanutils.converters.DateConverter toDate

 45          *警告:     (N.B. Re-configure Converter or use alternative implementation)

 46          */

 47         System.out.println(p.getName());

 48         System.out.println(p.getAge());

 49 //        System.out.println(p.getBirthday());

 50     }

 51     

 52     /**

 53      * 

 54      * @功能:在没有相应自动转换的时候需要注册一个转换器来转换想要的类型

 55      *             test2()为一个自定义的转换器,作为例子来写

 56      *             注:Apache已经写好了一些转换器,需要的时候看看文档converter下有没有想要的转换器

 57      *                 test3()介绍了date转换的转换器,只需要注册一下即可

 58      * @throws IllegalAccessException

 59      * @throws InvocationTargetException

 60      * @日期:2013-10-24

 61      */

 62     @Test

 63     public void test2() throws IllegalAccessException, InvocationTargetException{

 64         //注册一个String类型到Date类型的转换器

 65         //通过查看API文档,可知Converter是一个接口,需要实现其方法,第二个参数为想要转换成的数据类型

 66         ConvertUtils.register(new Converter() {

 67             

 68             public Object convert(Class type, Object value) {

 69                 

 70                 //判断是不是String类型的数据,不是则抛出异常

 71                 if(!(value instanceof String)){

 72                     throw new ConversionException("不是String数据类型!");

 73                 }

 74                 //是String的话,把Object的value强转成String

 75                 String strValue = (String) value;

 76                 //判断是不是一个空字符串

 77                 if(strValue.trim().equals("")){

 78                     return null;

 79                 }

 80                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

 81                 try {

 82                     return sdf.parse(strValue);

 83                 } catch (ParseException e) {

 84                     throw new RuntimeException(e);

 85                 }

 86             }

 87         }, Date.class);

 88         

 89         Person p = new Person();

 90         String date = "1988-09-23";

 91         BeanUtils.setProperty(p, "birthday", date);

 92         System.out.println(p.getBirthday());

 93     }

 94     

 95     /**

 96      * 使用框架里已定义好的转换器

 97      * @throws InvocationTargetException 

 98      * @throws IllegalAccessException 

 99      * 

100      */

101     @Test

102     public void test3() throws IllegalAccessException, InvocationTargetException{

103         Person p = new Person();

104         String date = "1980-03-04";

105         //注册一个转换器,将String类型的date转为Date型

106         ConvertUtils.register(new DateLocaleConverter(), Date.class);

107         BeanUtils.setProperty(p, "birthday", date);

108         //.toLocaleString()转为需要的日期格式

109         System.out.println(p.getBirthday().toLocaleString());

110     }

111     /*

112      * 注:虽然,可以用已经写好的转换器,但是这个写好的转换器存在一个问题,就是date为空的时候,

113      * 它检测不到,运行时会抛出异常,所以最好自己学会去写一个转换器

114      */

115     

116     

117     /**

118      * 用map集合中的值,填充bean的属性值

119      * @throws Exception 

120      * @throws IllegalAccessException 

121      */

122     @Test

123     public void test4() throws IllegalAccessException, Exception{

124         Person p = new Person();

125         Map map = new HashMap();

126         map.put("name", "张三");

127         map.put("age", "24");

128         map.put("birthday", "1978-09-27");

129         //注册一个日期转换器

130         ConvertUtils.register(new DateLocaleConverter(), Date.class);

131         //将map集合中的值填充到bean的属性,map的key值必须要与bean的属性相同才能填充进去

132         BeanUtils.populate(p, map);

133         System.out.println("name:" + p.getName() + "\r\n" + "age:" + p.getAge() + "\r\n" + "birthday:" + p.getBirthday().toLocaleString().substring(0,10));

134     }

135 }

 

你可能感兴趣的:(BeanUtils)