spring mvc出现 Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'
在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错:
19:41:01,234 WARN DefaultHandlerExceptionResolver:354 - Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException
解决方法:
在相关的Controller类中添加
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}