Spring MVC 架构,前端表单包含时间的字符串传进去,转换成日期时报错了
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'tbUcTask' on field 'nextExecTime': rejected value [2016-01-07 00:00:00]; codes [typeMismatch.tbUcTask.nextExecTime,typeMismatch.nextExecTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tbUcTask.nextExecTime,nextExecTime]; arguments []; default message [nextExecTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'nextExecTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2016-01-07 00:00:00'; nested exception is java.lang.IllegalArgumentException]
解决方法:
在相应的控制器里添加以下方法
@InitBinder protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
Leave a Reply