侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

JSP 开发之Spring BeanUtils组件使用

Java  /  管理员 发布于 7年前   245

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属性复制情况

@Testpublic 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属性的复制情况

@Testpublic 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中属性值的复制情况

@Testpublic 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);}

测试

@Testpublic 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]namebirth

以上就是JSP中Spring BeanUtils组件的使用,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


  • 上一条:
    Jsp+Servlet实现购物车功能
    下一条:
    JSP 开发之Spring Security详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在java中实现的脱敏工具类代码示例分享(0个评论)
    • zookeeper安装流程步骤(0个评论)
    • 在java中你背的“八股文”可能已经过时了(2个评论)
    • 在php8.0+版本中使用属性来增加值代码示例(3个评论)
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2018-03
    • 2020-03
    • 2023-05
    • 2023-11
    • 2024-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客