首页 > 其他 > 详细

BeanUtils.copyProperties方法,当属性Date为null解决

时间:2017-09-03 01:01:00      阅读:1524      评论:0      收藏:0      [点我收藏+]

问题描述:org.apache.commons.beanutils

user对象和formBean对象都有属性birthday,而且都是java.sql.Date类型的

当进行BeanUtils.copyProperties(user,formBean);复制属性时候,formBean中属性birthday为null,那么就会出现如下异常

技术分享

解决方法

在BeanUtils.copyProperties(user,formBean);前加上一行代码:

BeanUtilsBean.getInstance().getConvertUtils().register(new SqlDateConverter(null),java.sql.Date.class);

也就是注册一个SqlDateConverter转换器,defaultValue=null,这样就可以接受null值,具体原因可以自己看源码理解

然后user中birthday属性值,往数据库表写数据(date型字段)时也不需要转换,因为是java.sql.Date

最后附上不错的文章: http://jen.iteye.com/blog/1032521 

BeanUtils.copyProperties方法,当属性Date为null解决

原文:http://www.cnblogs.com/zhima-hu/p/7468488.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!