首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

spring mvc 表单映射date类型字段的有关问题

2012-08-25 
spring mvc 表单映射date类型字段的问题在mvc中如果表单属性的类型是日期型时,从页面绑定字符串数据会出错

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方法

@InitBinderprotected 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日期格式与页面日期格式要一致!

热点排行