转自:https://www.cnblogs.com/liaojie970/p/5566388.htmlhtml
在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错:java
Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'expert.birthdate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'birthdate': no matching editors or conversion strategy found spring
解决方法一:mvc
1.使对应Controller控制器继承 extends SimpleFormController
2.重写initBinder方法 ui
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); }
注意SimpleDateFormat日期格式与页面日期格式要一致!spa
解决方法二:code
Spring3.0以上的SimpleFormController 已通过时了,最新方式是使用@InitBinder注解的方式orm
在对应的Controller控制器中htm
@InitBinder protected void init(HttpServletRequest request, ServletRequestDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); }