Converter转换器使用

时间:2023-03-08 20:14:20
Converter转换器使用
 package com.xu.javabean;

 import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; public class Test { // Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景
// 开发了一套简单、易用的API操作Bean的属性——BeanUtils,
// 在Beanutil中可以直接进行类型的自动转换。
// 使用beanUtils操作bean的属性(首先导入jar包,第三方开发工具包) @org.junit.Test
public void test1() throws Exception {
// BeanUtils可以填充JavaBeans属性通过反射实用方法。 Person p = new Person();
// void org.apache.commons.beanutils.BeanUtils
// .setProperty(Object bean, String name, Object value)
// throws IllegalAccessException, InvocationTargetException
// 设置指定的属性值,进行类型转换为所需的符合目标的属性类型。
BeanUtils.setProperty(p, "name", "zhangsan");
System.out.println(p.getName());
} @org.junit.Test
public void test2() throws IllegalAccessException,
InvocationTargetException {
// 例如:接收用户浏览器传递过来的姓名、年龄、性别等信息
// 由于是表单提交,所以都是字符串
String name = "lisi";
String age = "67";
String sex = "男";
String birthday = "1989-09-16"; // 为了让日期赋到bean的birthday属性上,我们给BeanUtils注册一个日期转换器
// ConvertUtils类转换为字符串的标量值来指定类的对象的实用方法,字符串数组来指定类的数组。 // static void register(Converter converter, Class clazz)
// 注册为指定目标类的自定义转换器,取代任何以前注册的转换器。
ConvertUtils.register(new Converter() {
// Object convert(Class type, Object value)
// 将指定的输入对象转换为指定类型的输出对象。 public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("只支持String类型的转换!");
}
String str = (String) value;
if (str.trim().equals("")) {
return null;
}
// SimpleDateFormat(String pattern)
// 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// Date parse(String source)
// 从给定字符串的开始解析文本,以生成一个日期。
try {
// 此方法会抛出异常,因为是匿名内部类实现接口,所以不能抛只能处理
return sdf.parse(str);
} catch (ParseException e) {
// 如果发生此类异常,那么就让程序停止,
// 必须加上参数e,因为异常链不能断,要让调用者看到异常信息
throw new RuntimeException(e);
}
}
}, Date.class); Person p = new Person();
BeanUtils.setProperty(p, "name", name);
// p对象的age属性应该接收int类型的数据,但是使用BeanUtils可以自动帮我们完成转换动作
// 只支持8种基本数据类型的转换
// 其实可以用 BeanUtils.populate(bean, properties);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "sex", sex);
BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getSex());
System.out.println(p.getBirthday());
} @org.junit.Test
public void test3() throws IllegalAccessException,
InvocationTargetException {
// 例如:接收用户浏览器传递过来的姓名、年龄、性别等信息
// 由于是表单提交,所以都是字符串
String name = "lisi";
String age = "67";
String sex = "男";
String birthday = "1989-09-16"; ConvertUtils.register(new DateLocaleConverter(), Date.class); Person p = new Person();
BeanUtils.setProperty(p, "name", name);
// p对象的age属性应该接收int类型的数据,但是使用BeanUtils可以自动帮我们完成转换动作
// 只支持8种基本数据类型的转换
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "sex", sex);
BeanUtils.setProperty(p, "birthday", birthday); System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getSex());
System.out.println(p.getBirthday());
} }
</span>