主页 > 知识库 > JSP 开发之Spring BeanUtils组件使用

JSP 开发之Spring BeanUtils组件使用

热门标签:团购网站 Linux服务器 服务器配置 科大讯飞语音识别系统 银行业务 阿里云 电子围栏 Mysql连接数设置

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();

  SetString> emptyNames = new HashSetString>();
  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组件的使用,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
  • 浅析Java中Apache BeanUtils和Spring BeanUtils的用法

标签:江苏 蚌埠 萍乡 广元 枣庄 衡水 衢州 大理

巨人网络通讯声明:本文标题《JSP 开发之Spring BeanUtils组件使用》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266