您的位置:首页 > 产品设计 > UI/UE

Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for pr

2014-04-26 12:28 621 查看
在springmvc中如果表单属性的类型是日期型时,从页面绑定字符串数据会出错

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

Java代码
@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日期格式与页面日期格式要一致!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐