有一个Person类
import java.util.Date; public class Person { private String name; private String password; private int age; private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import java.lang.reflect.InvocationTargetException; import java.util.Date; import java.util.HashMap; import java.util.Map; 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; import org.junit.Test; public class Demo1 { @Test public void test1() throws IllegalAccessException, InvocationTargetException{ Person p = new Person(); BeanUtils.setProperty(p, "name", "小明"); System.out.println(p.getName()); } @Test public void test2() throws IllegalAccessException, InvocationTargetException{ //假设有表单传来数据 String name = "zhangsan"; String password = "password"; String age = "24"; Person p = new Person(); BeanUtils.setProperty(p, "name", name); BeanUtils.setProperty(p, "password", password); BeanUtils.setProperty(p, "age", age); //自动进行了数据类型的转化,只支持8种基本类型 System.out.println(p.getName()); System.out.println(p.getPassword()); System.out.println(p.getAge()); } //以下是编写时间的转化器 @Test public void test3() throws IllegalAccessException, InvocationTargetException{ String birthday = "1990-03-16"; //让日期赋到bean的日期属性上,需注册转换器 ConvertUtils.register(new Converter(){ public Object convert(Class type, Object value) { if(value==null){ return null; } if(!(value instanceof String)){ throw new ConversionException("only support the type of String"); } String str = (String) value; if(str.trim().equals("")){ return null; } SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { return df.parse(str); } catch (ParseException e) { throw new RuntimeException(e); //异常链不能断 } } }, Date.class); Person p = new Person(); BeanUtils.setProperty(p,"birthday",birthday); //转成时间显示格式 System.out.println(p.getBirthday().toLocaleString()); } //使用Apache给的时间转换器,当你把时间赋值空时有异常 @Test public void test4() throws IllegalAccessException, InvocationTargetException{ String birthday = "1990-03-16"; ConvertUtils.register(new DateLocaleConverter(),Date.class); Person p = new Person(); BeanUtils.setProperty(p,"birthday",birthday); System.out.println(p.getBirthday()); } //map集合的操作 @Test public void test5() throws IllegalAccessException, InvocationTargetException{ Map map = new HashMap(); map.put("name", "zhangsan"); map.put("password", "123"); map.put("age", 24); map.put("birthday", "1990-04-17"); ConvertUtils.register(new DateLocaleConverter(),Date.class); Person bean = new Person(); //填充进去 BeanUtils.populate(bean, map); System.out.println(bean.getName()); System.out.println(bean.getPassword()); System.out.println(bean.getAge()); System.out.println(bean.getBirthday()); } }