使用beanUtils操作javabean

//使用beanUtils操作javabean


package cn.itcast.beanUtils;


import java.lang.reflect.InvocationTargetException;
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.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;


public class Demo1 {


@Test
public void test1() throws IllegalAccessException, InvocationTargetException {
Person p = new Person();
BeanUtils.setProperty(p, "name", "xcc");
System.out.println(p.getName());
}
//下面的代码是有问题的,因为benaUtils框架只支持基本暑假类型
@Test
public void test2() throws IllegalAccessException, InvocationTargetException {
String name = "aaa";
String password = "123";
String age = "123";

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);//只支持8种基本类型

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}
@Test
public void test3() throws IllegalAccessException, InvocationTargetException {
String name = "aaa";
String password = "123";
String age = "123";
String birthday = "1992-8-22";

//为了让日期赋到bena的birthday属性上,我们给beanutils注册一个日期转换器
ConvertUtils.register(new Converter(){
@Override
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 df = new SimpleDateFormat("yy-MM-dd");
try{
return df.parse(str);
}catch (ParseException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}

}, Date.class);

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);//只支持8种基本类型
BeanUtils.setProperty(p, "birthday", birthday);

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
Date date = p.getBirthday();
System.out.println(date.toLocaleString());
}
@Test
public void test4() throws IllegalAccessException, InvocationTargetException {
String name = "aaa";
String password = "123";
String age = "21";
String birthday = "1992-8-22";
ConvertUtils.register(new DateLocaleConverter(), Date.class);

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);//只支持8种基本类型
BeanUtils.setProperty(p, "birthday", birthday);

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
Date date = p.getBirthday();
System.out.println(date.toLocaleString());
}

}



//使用beanUtils操作javabean


package cn.itcast.beanUtils;


import java.lang.reflect.InvocationTargetException;
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.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;


public class Demo1 {


@Test
public void test1() throws IllegalAccessException, InvocationTargetException {
Person p = new Person();
BeanUtils.setProperty(p, "name", "xcc");
System.out.println(p.getName());
}
//下面的代码是有问题的,因为benaUtils框架只支持基本暑假类型
@Test
public void test2() throws IllegalAccessException, InvocationTargetException {
String name = "aaa";
String password = "123";
String age = "123";

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);//只支持8种基本类型

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}
@Test
public void test3() throws IllegalAccessException, InvocationTargetException {
String name = "aaa";
String password = "123";
String age = "123";
String birthday = "1992-8-22";

//为了让日期赋到bena的birthday属性上,我们给beanutils注册一个日期转换器
ConvertUtils.register(new Converter(){
@Override
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 df = new SimpleDateFormat("yy-MM-dd");
try{
return df.parse(str);
}catch (ParseException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}

}, Date.class);

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);//只支持8种基本类型
BeanUtils.setProperty(p, "birthday", birthday);

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
Date date = p.getBirthday();
System.out.println(date.toLocaleString());
}
@Test
public void test4() throws IllegalAccessException, InvocationTargetException {
String name = "aaa";
String password = "123";
String age = "21";
String birthday = "1992-8-22";
ConvertUtils.register(new DateLocaleConverter(), Date.class);

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);//只支持8种基本类型
BeanUtils.setProperty(p, "birthday", birthday);

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
Date date = p.getBirthday();
System.out.println(date.toLocaleString());
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值