使用beanUtils操作javabean

来源:互联网 发布:工程量清单软件 编辑:程序博客网 时间:2024/05/16 07:23
//使用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());
}
}


0 0
原创粉丝点击