用内省操作javabean较为麻烦,我们使用beanUtils操作javabean。因为beanUtils是第三方Apache开发的,所以我们需要导入beanUtils相关的开发包(第三方jar包)。步骤:在我们的工程下新建一个Floder,名为lib,再拷贝commons-beanutils-1.8.3.jar。但是因为beanUtils这个jar包在工作过程中,需要一个日志记录器的支持,那么还需要将它的支持jar拷贝过来,即commons_logging.jar。然后将这两个jar包加入到构建路径(classpath)中,方法是选中这两个jar包,右键点击build path,点击add to build path即可。
定义一个Person类:
package beanUtils; import java.util.Date; public class Person { //Person类就可以称为一个javabean private String name; //字段(没有提供get和set方法就不能称为属性) 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() { //只要字段对外提供了get或set方法就可以称为属性(javabean的属性) 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; } public String getAb(){ //javabean含有ab属性(因为含有get方法,即使没有定义ab字段) return null; } }操作这个bean:
package beanUtils; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; 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; //使用beanUtils操作bean的属性(第三方) public class Demo1 { //设置属性的值 @Test public void test1() throws IllegalAccessException, InvocationTargetException{ Person p=new Person(); //创建一个p对象 BeanUtils.setProperty(p, "name", "xcc"); //设置p对象的name属性的值为xcc System.out.println("test1:"); System.out.println(p.getName()); System.out.println(); } //获取表单提交的数据 @Test public void test2() throws IllegalAccessException, InvocationTargetException{ //服务器端获取用户在表单提交的数据,全都是String类型 String name="aaaa"; String password="123"; String age="34"; //将数据封装成对象 Person p=new Person(); BeanUtils.setProperty(p,"name",name ); //将数据封装到这个bean里 BeanUtils.setProperty(p,"password",password ); BeanUtils.setProperty(p,"age", age); //BeanUtils自动将String类型数据转换成int型,但是这种转换只支持8种基本数据类型 System.out.println("test2:"); System.out.println(p.getName()); System.out.println(p.getPassword()); System.out.println(p.getAge()); System.out.println(); } /* * BeanUtils只支持基本数据类型的转换,我们在Person中定义了一个Date类型的属性birthday,在接受用户通过表单提交的数据时, * BeanUtils不能将String类型的birthday转换成Date类型的birthday。但是我们期望程序能这样做,这时候, * 我们要给BeanUtils注册日期转换器,让它使用我们自定义的转换器。 * */ @Test public void test3() throws IllegalAccessException, InvocationTargetException{ //服务器端获取用户在表单提交的数据,全都是String类型 String name="aaaa"; String password="123"; String age="34"; String birthday="1982-09-09"; //为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器 ConvertUtils.register(new Converter(){ //创建一个转换器在,这里要导入源码commons-beanutils-1.8.3-sources.jar /* * 这里导入源码的方式是: * 在Converter接口下,按F2,点击open declaration,添加一个存在的文件,就是这里的commons-beanutils-1.8.3-sources.jar * */ public Object convert(Class type, Object value) { //Converter是一个接口(有未实现的抽象方法),不能直接new,所以要实现它的方法 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("yyyy-MM-dd"); //年-月-日 try { return df.parse(str); //字符串转换为日期。这里是继承父类的方法,不能比父类抛出更多的异常,所以不能抛出异常,只能try-catch } catch (ParseException e) { throw new RuntimeException(e); //异常链(e)不能断 ,e是异常信息,要封装在异常里 } } }, Date.class); //给beanUtils注册一个转换Date类型数据的转换器 //将数据封装成对象 Person p=new Person(); BeanUtils.setProperty(p,"name",name ); //将数据封装到这个bean里 BeanUtils.setProperty(p,"password",password ); BeanUtils.setProperty(p,"age", age); //BeanUtils自动将String类型数据转换成int型,但是这种转换只支持8种基本数据类型 BeanUtils.setProperty(p, "birthday", birthday); System.out.println("test3:"); System.out.println(p.getName()); System.out.println(p.getPassword()); System.out.println(p.getAge()); System.out.println(p.getBirthday()); System.out.println(); } /* * 实际在开发过程中我们没必要自己写转换器,Apache为我们提供了一些已经写好的转换器 * */ @Test public void test4() throws IllegalAccessException, InvocationTargetException{ //服务器端获取用户在表单提交的数据,全都是String类型 String name="aaaa"; String password="123"; String age="34"; String birthday="1982-09-09"; //Apache提供写好的日期转换器DateLocaleConverter,但是这个转换器存在bug,不能检测到用户输入的生日为空 ConvertUtils.register(new DateLocaleConverter(), Date.class); //注册一个转换器 //将数据封装成对象 Person p=new Person(); BeanUtils.setProperty(p,"name",name ); //将数据封装到这个bean里 BeanUtils.setProperty(p,"password",password ); BeanUtils.setProperty(p,"age", age); //BeanUtils自动将String类型数据转换成int型,但是这种转换只支持8种基本数据类型 BeanUtils.setProperty(p, "birthday", birthday); System.out.println("test4:"); System.out.println(p.getName()); System.out.println(p.getPassword()); System.out.println(p.getAge()); System.out.println(p.getBirthday()); System.out.println(); } //用map集合中的值,填充bean的属性 @Test public void test5() throws IllegalAccessException, InvocationTargetException{ //request:客户端请求 Map map=new HashMap(); //客户提交的数据使用map集合封装 map.put("name", "aaaa"); map.put("password", "123"); map.put("age", "23"); map.put("birthday", "1982-09-09"); //将map中的数据整合到bean中 ConvertUtils.register(new DateLocaleConverter(), Date.class); //注册转换器 Person bean=new Person(); BeanUtils.populate(bean, map); //把map集合数据填充到person这个bean上去 System.out.println("test5:"); System.out.println(bean.getName()); System.out.println(bean.getPassword()); System.out.println(bean.getAge()); System.out.println(bean.getBirthday()); } }运行结果: