芝麻_糊

导航

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

问题描述: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 

posted on 2017-09-03 00:09  芝麻_糊  阅读(4719)  评论(0编辑  收藏  举报