問題描述: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