无为清净楼资源网 Design By www.qnjia.com
JSP 开发之Spring BeanUtils组件使用
用于演示的javabean
import java.util.Date; public class People { private String name; private int age; private Date birth; public People(String name, int age, Date birth) { super(); this.name = name; this.age = age; this.birth = birth; } public People() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "People [name=" + name + ", age=" + age + ", birth=" + birth + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } }
测试(所有测试只与源javabean属性值有关,与目标javabean属性值无关)
当源javabean属性均有值时的目标javabean属性复制情况
@Test public void springBeanUtilsTest(){ People oldPeople = new People("oldName",100,new Date()); People newPeople = new People(); //BeanUtils.copyProperties(Object source,Object target); BeanUtils.copyProperties(oldPeople, newPeople); System.out.println(oldPeople); System.out.println(newPeople); }
输出结果如下
People [name=oldName, age=100, birth=Wed Jul 19 18:46:13 CST 2017] People [name=oldName, age=100, birth=Wed Jul 19 18:46:13 CST 2017]
当源javabean非Date类型的属性值为null时目标javabean属性的复制情况
@Test public void springBeanUtilsTest(){ People oldPeople = new People(null,100,new Date()); People newPeople = new People("newName",20,null); //BeanUtils.copyProperties(Object source,Object target); BeanUtils.copyProperties(oldPeople, newPeople); System.out.println(oldPeople); System.out.println(newPeople); }
输出结果如下
注意:目标javabean中的非null属性值被覆盖为null了
People [name=null, age=100, birth=Wed Jul 19 19:04:48 CST 2017] People [name=null, age=100, birth=Wed Jul 19 19:04:48 CST 2017]
当源javabean中Date类型的属性值为null时目标javabean中属性值的复制情况
@Test public void springBeanUtilsTest(){ People oldPeople = new People("oldName",100,null); People newPeople = new People("newName",20,new Date()); //BeanUtils.copyProperties(Object source,Object target); 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(Object source,Object target);方法有一个不足的地方,就是当source里的属性对应的属性值为null时,也会覆盖掉target里相同属性名的属性,即使target中该属性值已存在且不为null的属性值,这显然有些不合理,这是我们可以使用它的一个重载方法:
BeanUtils.copyProperties(Object source,Object target, String... ignoreProperties);
最后一个参数的含义是,复制属性值时忽略的属性名称,所有我们只要找出source中属性值为null的属性名称数组即可,方法如下:
/** * * @Title: getNullPropertyNames * @Description: 获取一个对象中属性值为null的属性名字符串数组 * @param source * @return */ public static String[] getNullPropertyNames (Object source) { final BeanWrapper src = new BeanWrapperImpl(source); java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors(); Set<String> emptyNames = new HashSet<String>(); for(java.beans.PropertyDescriptor pd : pds) { Object srcValue = src.getPropertyValue(pd.getName()); if (srcValue == null) emptyNames.add(pd.getName()); } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); }
测试
@Test public void copyBeanNotNull(){ People oldPeople = new People(null, 100, null); People newPeople = new People("newName", 20, new Date()); //BeanUtils.copyProperties(Object source,Object target, String... ignoreProperties); BeanUtils.copyProperties(oldPeople, newPeople, getNullPropertyNames(oldPeople)); System.out.println(oldPeople); System.out.println(newPeople); for(String key : getNullPropertyNames(oldPeople)){ System.out.println(key); } }
输出结果如下
People [name=null, age=100, birth=null] People [name=newName, age=100, birth=Wed Jul 19 23:31:05 CST 2017] name birth
以上就是JSP中Spring BeanUtils组件的使用,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]