I'm analyzing spring-mvc-showcase example project (spring-mvc-showcase github). Confused with the way validation response is presented on JSP page when I hit incorrect date format (Birth Date field on screenshot). How can I make it more user friendly with some custom message, without those ConversionFailedException details?
screenshot:

Annotation-driven validation is applied. Down below is code segment from bean class representing birthDate field.
FormBean.java
@DateTimeFormat(iso=ISO.DATE)
@Past
private Date birthDate;
Method responsible for form submit:
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";
}
}
Note, that you are working with binding errors here. These are thrown long before the actual JSR-303 validation is performed and they override JSR-303 constraint violations for the failed field.
Codes for the binding errors are typeMismatch. So you can add for example this to your messages properties:
typeMismatch.birthDate = Invalid birth date format.
Check JavaDoc for DefaultMessageCodesResolver and DefaultBindingErrorProcessor to discover how Spring's error code resolution works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With