JSP 开发之Spring BeanUtils组件使用
JSP开发之SpringBeanUtils组件使用
用于演示的javabean
importjava.util.Date; publicclassPeople{ privateStringname; privateintage; privateDatebirth; publicPeople(Stringname,intage,Datebirth){ super(); this.name=name; this.age=age; this.birth=birth; } publicPeople(){ super(); //TODOAuto-generatedconstructorstub } @Override publicStringtoString(){ return"People[name="+name+",age="+age+",birth="+birth+"]"; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicDategetBirth(){ returnbirth; } publicvoidsetBirth(Datebirth){ this.birth=birth; } }
测试(所有测试只与源javabean属性值有关,与目标javabean属性值无关)
当源javabean属性均有值时的目标javabean属性复制情况
@Test publicvoidspringBeanUtilsTest(){ PeopleoldPeople=newPeople("oldName",100,newDate()); PeoplenewPeople=newPeople(); //BeanUtils.copyProperties(Objectsource,Objecttarget); BeanUtils.copyProperties(oldPeople,newPeople); System.out.println(oldPeople); System.out.println(newPeople); }
输出结果如下
People[name=oldName,age=100,birth=WedJul1918:46:13CST2017] People[name=oldName,age=100,birth=WedJul1918:46:13CST2017]
当源javabean非Date类型的属性值为null时目标javabean属性的复制情况
@Test publicvoidspringBeanUtilsTest(){ PeopleoldPeople=newPeople(null,100,newDate()); PeoplenewPeople=newPeople("newName",20,null); //BeanUtils.copyProperties(Objectsource,Objecttarget); BeanUtils.copyProperties(oldPeople,newPeople); System.out.println(oldPeople); System.out.println(newPeople); }
输出结果如下
注意:目标javabean中的非null属性值被覆盖为null了
People[name=null,age=100,birth=WedJul1919:04:48CST2017] People[name=null,age=100,birth=WedJul1919:04:48CST2017]
当源javabean中Date类型的属性值为null时目标javabean中属性值的复制情况
@Test publicvoidspringBeanUtilsTest(){ PeopleoldPeople=newPeople("oldName",100,null); PeoplenewPeople=newPeople("newName",20,newDate()); //BeanUtils.copyProperties(Objectsource,Objecttarget); BeanUtils.copyProperties(oldPeople,newPeople); System.out.println(oldPeople); System.out.println(newPeople); }
输出结果如下
People[name=oldName,age=100,birth=null] People[name=oldName,age=100,birth=null]
BeanUtils.copyProperties(Objectsource,Objecttarget);方法有一个不足的地方,就是当source里的属性对应的属性值为null时,也会覆盖掉target里相同属性名的属性,即使target中该属性值已存在且不为null的属性值,这显然有些不合理,这是我们可以使用它的一个重载方法:
BeanUtils.copyProperties(Objectsource,Objecttarget,String...ignoreProperties);
最后一个参数的含义是,复制属性值时忽略的属性名称,所有我们只要找出source中属性值为null的属性名称数组即可,方法如下:
/** * *@Title:getNullPropertyNames *@Description:获取一个对象中属性值为null的属性名字符串数组 *@paramsource *@return */ publicstaticString[]getNullPropertyNames(Objectsource){ finalBeanWrappersrc=newBeanWrapperImpl(source); java.beans.PropertyDescriptor[]pds=src.getPropertyDescriptors(); SetemptyNames=newHashSet (); for(java.beans.PropertyDescriptorpd:pds){ ObjectsrcValue=src.getPropertyValue(pd.getName()); if(srcValue==null)emptyNames.add(pd.getName()); } String[]result=newString[emptyNames.size()]; returnemptyNames.toArray(result); }
测试
@Test publicvoidcopyBeanNotNull(){ PeopleoldPeople=newPeople(null,100,null); PeoplenewPeople=newPeople("newName",20,newDate()); //BeanUtils.copyProperties(Objectsource,Objecttarget,String...ignoreProperties); BeanUtils.copyProperties(oldPeople,newPeople,getNullPropertyNames(oldPeople)); System.out.println(oldPeople); System.out.println(newPeople); for(Stringkey:getNullPropertyNames(oldPeople)){ System.out.println(key); } }
输出结果如下
People[name=null,age=100,birth=null] People[name=newName,age=100,birth=WedJul1923:31:05CST2017] name birth
以上就是JSP中SpringBeanUtils组件的使用,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!