码迷,mamicode.com
首页 > 其他好文 > 详细

Converter转换器使用

时间:2015-02-25 22:21:46      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.xu.javabean;
  2 
  3 import java.lang.reflect.InvocationTargetException;
  4 import java.util.Date;
  5 import java.text.ParseException;
  6 import java.text.SimpleDateFormat;
  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 
 14 public class Test {
 15 
 16     // Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景
 17     // 开发了一套简单、易用的API操作Bean的属性——BeanUtils,
 18     // 在Beanutil中可以直接进行类型的自动转换。
 19     // 使用beanUtils操作bean的属性(首先导入jar包,第三方开发工具包)
 20 
 21     @org.junit.Test
 22     public void test1() throws Exception {
 23         // BeanUtils可以填充JavaBeans属性通过反射实用方法。
 24 
 25         Person p = new Person();
 26         // void org.apache.commons.beanutils.BeanUtils
 27         // .setProperty(Object bean, String name, Object value)
 28         // throws IllegalAccessException, InvocationTargetException
 29         // 设置指定的属性值,进行类型转换为所需的符合目标的属性类型。
 30         BeanUtils.setProperty(p, "name", "zhangsan");
 31         System.out.println(p.getName());
 32     }
 33 
 34     @org.junit.Test
 35     public void test2() throws IllegalAccessException,
 36             InvocationTargetException {
 37         // 例如:接收用户浏览器传递过来的姓名、年龄、性别等信息
 38         // 由于是表单提交,所以都是字符串
 39         String name = "lisi";
 40         String age = "67";
 41         String sex = "男";
 42         String birthday = "1989-09-16";
 43 
 44         // 为了让日期赋到bean的birthday属性上,我们给BeanUtils注册一个日期转换器
 45         // ConvertUtils类转换为字符串的标量值来指定类的对象的实用方法,字符串数组来指定类的数组。
 46 
 47         // static void register(Converter converter, Class clazz)
 48         // 注册为指定目标类的自定义转换器,取代任何以前注册的转换器。
 49         ConvertUtils.register(new Converter() {
 50             // Object convert(Class type, Object value)
 51             // 将指定的输入对象转换为指定类型的输出对象。
 52 
 53             public Object convert(Class type, Object value) {
 54                 if (value == null) {
 55                     return null;
 56                 }
 57                 if (!(value instanceof String)) {
 58                     throw new ConversionException("只支持String类型的转换!");
 59                 }
 60                 String str = (String) value;
 61                 if (str.trim().equals("")) {
 62                     return null;
 63                 }
 64                 // SimpleDateFormat(String pattern)
 65                 // 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
 66                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 67                 // Date parse(String source)
 68                 // 从给定字符串的开始解析文本,以生成一个日期。
 69                 try {
 70                     // 此方法会抛出异常,因为是匿名内部类实现接口,所以不能抛只能处理
 71                     return sdf.parse(str);
 72                 } catch (ParseException e) {
 73                     // 如果发生此类异常,那么就让程序停止,
 74                     // 必须加上参数e,因为异常链不能断,要让调用者看到异常信息
 75                     throw new RuntimeException(e);
 76                 }
 77             }
 78         }, Date.class);
 79 
 80         Person p = new Person();
 81         BeanUtils.setProperty(p, "name", name);
 82         // p对象的age属性应该接收int类型的数据,但是使用BeanUtils可以自动帮我们完成转换动作
 83         // 只支持8种基本数据类型的转换
 84         // 其实可以用 BeanUtils.populate(bean, properties);
 85         BeanUtils.setProperty(p, "age", age);
 86         BeanUtils.setProperty(p, "sex", sex);
 87         BeanUtils.setProperty(p, "birthday", birthday);
 88 
 89         System.out.println(p.getName());
 90         System.out.println(p.getAge());
 91         System.out.println(p.getSex());
 92         System.out.println(p.getBirthday());
 93     }
 94 
 95     @org.junit.Test
 96     public void test3() throws IllegalAccessException,
 97             InvocationTargetException {
 98         // 例如:接收用户浏览器传递过来的姓名、年龄、性别等信息
 99         // 由于是表单提交,所以都是字符串
100         String name = "lisi";
101         String age = "67";
102         String sex = "男";
103         String birthday = "1989-09-16";
104 
105         ConvertUtils.register(new DateLocaleConverter(), Date.class);
106 
107         Person p = new Person();
108         BeanUtils.setProperty(p, "name", name);
109         // p对象的age属性应该接收int类型的数据,但是使用BeanUtils可以自动帮我们完成转换动作
110         // 只支持8种基本数据类型的转换
111         BeanUtils.setProperty(p, "age", age);
112         BeanUtils.setProperty(p, "sex", sex);
113         BeanUtils.setProperty(p, "birthday", birthday);
114 
115         System.out.println(p.getName());
116         System.out.println(p.getAge());
117         System.out.println(p.getSex());
118         System.out.println(p.getBirthday());
119     }
120 
121 }
122 </span>

 

Converter转换器使用

标签:

原文地址:http://www.cnblogs.com/zhuolu/p/4300316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!