我在分析spring-mvc-showcase示例项目(spring-mvc-showcase github)。当我点击不正确的日期格式(截图上的出生日期字段)时,与在JSP页面上呈现验证响应的方式相混淆。如何使用一些自定义消息使用户更友好,但没有ConversionFailedException细节?注释表单验证 - 转换失败的自定义消息
截图:
注解驱动验证应用。下面是代表birthDate字段的bean类中的代码段。
FormBean.java
@DateTimeFormat(iso=ISO.DATE)
@Past
private Date birthDate;
方法负责提交表单:
FormController.java
@RequestMapping(method=RequestMethod.POST)
public String processSubmit(@Valid FormBean formBean, BindingResult result,
@ModelAttribute("ajaxRequest") boolean ajaxRequest,
Model model, RedirectAttributes redirectAttrs) {
if (result.hasErrors()) {
return null;
}
// Typically you would save to a db and clear the "form" attribute from the session
// via SessionStatus.setCompleted(). For the demo we leave it in the session.
String message = "Form submitted successfully. Bound " + formBean;
// Success response handling
if (ajaxRequest) {
// prepare model for rendering success message in this request
model.addAttribute("message", message);
return null;
} else {
// store a success message for rendering on the next request after redirect
// redirect back to the form to render the success message along with newly bound values
redirectAttrs.addFlashAttribute("message", message);
return "redirect:/form";
}
}
请显示您的看法。 –
这是公共github的例子,我粘贴链接在我的帖子的开头。这里是确切的看法form.jsp: [链接](https://github.com/spring-projects/spring-mvc-showcase/blob/master/src/main/webapp/WEB-INF/views/form。 jsp) – shx