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));  

}  

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

猜你喜欢

转载自blog.csdn.net/cling_snail/article/details/80022338