import java.util.Date;
enum Gender{
MAN,WOMAN;
}
public class PersonTest {
private String name;
private int age;
private Gender gender;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
这里定义了两个转换器:
<span style="white-space:pre"> </span>一个是日期的:Date
<span style="white-space:pre"> </span>另一个是枚举的:Enum
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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.junit.Test;
public class BeanUtilsTest {
@Test
public void test() throws Exception {
//PersonTest person = new PersonTest();
Class clazz = Class.forName("cn.itcast.beanutils.PersonTest");
PersonTest p = (PersonTest) clazz.newInstance();
String name = "Sheamus";
String age = "12";
String birthday = "1992-11-29";
String gender = "MAN";
//注册一个Date转化器
ConvertUtils.register(new Converter() {
/**
* @param type Data type to which this value should be converted
* 要转成的类型
* @param value 要转化的字符串的值。
* 转化之前的值
* @return The converted value ,转化后的值
*
*/
@Override
public Object convert(Class type, Object value) {
// TODO Auto-generated method stub
//在转换之前一定要判断数据的正确性
//首先判断传进来的值是否为空,如果为空,直接返回
//接着判断要转化的值是否是String类型的类型,如果不是就抛出一个异常
//最后,判断要转换的值是否为空。
//只有通过这3个限制才能进行转换
if(value == null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("我只转化String类型");
}
String val = (String)value;
if(val.trim().equals("")){
return null;
}
//将String转换成Date对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date time = null;
try {
time = (Date)sdf.parse(val);
} catch (ParseException e) {
// TODO Auto-generated catch block
//出错时一定要让虚拟机终止,不能打印出来,
//异常链不能断
throw new RuntimeException(e);
}
return time;
}
}, Date.class);
//注册一个枚举转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
// TODO Auto-generated method stub
if(value == null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只能转化String类型");
}
String val = (String)value;
if(val.trim().equals("")){
return null;
}
Gender gender = Enum.valueOf(Gender.class, val);
return gender;
}
},Gender.class);
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday);
BeanUtils.setProperty(p, "gender", gender);
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getBirthday().toLocaleString());
System.out.println(p.getGender());
}
}
个人感觉:
BeanUtils设计的报错功能太差,报的错误让你摸不着头脑。下次用的时候一定要注意导包和里面的参数的意义。