Spring表单提交日期类型报错,不进后台Controller层,ailed to convert from type java.lang.String to type java.util.Date

    问题原因,前台表单提交到了Date格式的数据,后台编码时不能识别该日期格式,故而无法进入到Controller就被拦截!

解决方法,在需要进行转换的Controller层加入如下代码,进行初始化转换。

    /* 
* 表单提交日期绑定 
*/  
@InitBinder  
public void initBinder(WebDataBinder binder) {  
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
   dateFormat.setLenient(false);  
   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  

}  

此时就可以获取到前台传来的日期类型的数据了。

发布了28 篇原创文章 · 获赞 7 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章