java之用beanUtils框架来操作bean

来源:互联网 发布:windows 访问samba 编辑:程序博客网 时间:2024/06/06 02:31
        下面是几段beanUtils框架来操作bean测试代码,其中bean是Person,其中这个框架是阿帕奇公司写的第三方,我们导入了相应的jar包,这几段代码体现出了手动写转换器的必要性,从理论上说明了问题
        但是从@Test3之后一直报错(相同的错我没有找出来),也希望高手大牛路过的时候能帮忙看看原因所在,我不胜感激!!



package beanstr;

import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

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 Introspector {
 private static final Object Date = null;

 // 使用beanUntils框架来操作bean(第三方),也是用来封装数据的
 // 使用beanUntils框架来操作bean(第三方)当把beanUntils导入的时候还需要它运行的api也就是commons
 // logging来支持
 @Test//操作单个值
 public void test1() throws IllegalAccessException,
   InvocationTargetException {
  Person p = new Person();
  BeanUtils.copyProperty(p, "name", "hyl");
  // 将某个bean的哪个值修改为什么值
  System.out.println(p.getName());
 }




 @Test//操作三个值
 public void test2() throws IllegalAccessException,
   InvocationTargetException {
  Person p = new Person();// 将用户提交的数据都封装起来
  String name = "hyl";
  String age = "12";
  String password = "123132";

  BeanUtils.setProperty(p, "name", name);
  BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
  BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型

  System.out.println(p.getName());
  System.out.println(p.getAge());
  System.out.println(p.getPassword());
 }




 @Test//重点是手动写了一个转换器,用来将字符串转换成Date类型
 public void test3() throws IllegalAccessException,
   InvocationTargetException {
  Person p = new Person();// 将用户提交的数据都封装起来
  String name = "hyl";
  String age = "12";
  String password = "123132";
  String birthday = "1995-02-15";

  // 为了让日期赋值到bean的birthday的属性上,我们给beanUntils注册一个日期转换器(这已经不再是sun公司的jar包了)
  ConvertUtils.register(new Converter() {

   public Object convert(Class type, Object value) {
    if (value == null) {
     return null;
    }
    if (!(value instanceof String)) {
     throw new ConversionException("请输入字符串");
    }
    String s = (String) value;
    if (s.trim().equals("")) {// trim()是去掉所有空格
     return null;
    }

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    try {// 由于不能抛出比父类更多的异常只能try
     return df.parse(s);
    } catch (ParseException e) {
     throw new RuntimeException(e); // 若有异常抛向上级,从而使程序停止执行;把异常信息封装进去保持异常链的连续性把导致异常的的异常封装起来
    }

   }

  }, Date.class);

  BeanUtils.setProperty(p, "name", name);
  BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
  BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型
  BeanUtils.setProperty(p, "birthday", birthday);// 而自动转换转换基本的8种类型

  System.out.println(p.getName());
  System.out.println(p.getAge());
  System.out.println(p.getPassword());
  System.out.println(p.getBiethday());// 打印出来是date.toString的特定格式

 }




 @Test//使用了阿帕奇公司的api
 public void test4() throws IllegalAccessException,
   InvocationTargetException {
  Person p = new Person();// 将用户提交的数据都封装起来
  String name = "hyl";
  String age = "12";
  String password = "123132";
  String birthday = "1995-02-15";

  // 使用阿帕奇公司写好的转换器
  ConvertUtils.register(new DateLocaleConverter(), (Class<?>) Date);

  BeanUtils.setProperty(p, "name", name);
  BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
  BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型
  BeanUtils.setProperty(p, "birthday", birthday);// 而自动转换转换基本的8种类型

  System.out.println(p.getName());
  System.out.println(p.getAge());
  System.out.println(p.getPassword());
  System.out.println(p.getBiethday().toLocaleString());// 打印出来是date.toString的特定格式
 }




 @Test//用map的集合填充bean的属性
 public void test5() throws IllegalAccessException,
   InvocationTargetException {
  Map map = new HashMap();
  map.put("name", "hyl");
  map.put("password", "123");
  map.put("age", "32");
  map.put("brithday", "1995-02-15");

  ConvertUtils.register(new DateLocaleConverter(), Date.class);
  Person p = new Person();
  BeanUtils.populate(p, map);//将map填充到person中
  System.out.println(p.getName());
  System.out.println(p.getAge());
  System.out.println(p.getPassword());
  System.out.println(p.getBiethday().toLocaleString());
 }

}
1 0
原创粉丝点击