【发布时间】:2014-07-10 13:28:26
【问题描述】:
在我的 Smpring MVC 应用程序中,我使用 SimpleDateFormat 作为 WebDataBinder 中的自定义编辑器来验证日期。当输入的日期与所需的模式不匹配时,我会在表单中收到原始错误消息:错误标记,例如:
Failed to convert property value of type java.lang.String to required type java.util.Date for property hireDate; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "432345"
我的问题是我想在 jsp 页面上显示自定义错误消息,例如:
“出生日期必须符合“dd/MM/yyyy”模式”
这是我的@InitBinder 的代码:
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
谢谢。
【问题讨论】:
标签: java validation spring-mvc simpledateformat spring-mvc-initbinders