spring mvc 表单映射date类型字段的有关问题
spring mvc 表单映射date类型字段的问题
在mvc中如果表单属性的类型是日期型时,从页面绑定字符串数据会出错
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
解决方法
1.控制器继承 extends SimpleFormController
2.重写initBinder方法
注意SimpleDateFormat日期格式与页面日期格式要一致!
在mvc中如果表单属性的类型是日期型时,从页面绑定字符串数据会出错
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
解决方法
1.控制器继承 extends SimpleFormController
2.重写initBinder方法
@InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); CustomDateEditor dateEditor = new CustomDateEditor(fmt, true); binder.registerCustomEditor(Date.class, dateEditor); super.initBinder(request, binder); }
注意SimpleDateFormat日期格式与页面日期格式要一致!