我在分析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