在我的 Smpring MVC 应用程序中,我使用 SimpleDateFormat 作为 WebDataBinder 中的自定义编辑器来验证日期。当输入的日期与所需的模式不匹配时,我在 form:errors 标签中收到原始错误消息,例如:
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));
}
谢谢。
最佳答案
尝试在您的消息包中注册以下消息:
typeMismatch.command.field=Date of birth must match "dd/MM/yyyy" pattern
or
typeMismatch.field = ...
用适当的命令对象替换命令和字段
关于java - 如何使用 Spring MVC 在 SimpleDateFormat 中指定自定义验证错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23791078/