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组件的使用,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新动态
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]