Spring MVC BindingResult access from inside a view

I am looking for a way to access BindingResultfrom a view (in my case a JSP page).

I have the following controller method:

@RequestMapping(value="/register/accept.html",method=RequestMethod.POST)
public ModelAndView doRegisterAccept(
  @Valid @ModelAttribute("registrationData") RegistrationData registrationData,
  BindingResult bindingResult
) {

  ModelAndView modelAndView         = new ModelAndView();
  modelAndView.addObject("bindingResultXXX", bindingResult);
  modelAndView.setViewName("users/registration/register");
  return modelAndView;

}

When doing this, it is RegistrationDatapopulated and errors are saved in BindingResult. So far, so good. However, I need to add BindingResultmanually in ModelAndViewso that it becomes visible in the view.

Is there a way to automatically add BindingResultto the model? I already tried setting the method signature to

public ModelAndView doRegisterAccept(
  @Valid @ModelAttribute("registrationData") RegistrationData registrationData,
  @ModelAttribute("bindingResult") BindingResult bindingResult
) {

where I was hoping, since any other parameter BindingResultwill be added to the model under the key BindingResult, but, unfortunately, this does not work.

So any ideas?

Adding

I just found that the results are really published using the key

org.springframework.validation.BindingResult.<NAME_OF_MODEL_ATTRIBUTE>

, Spring?!

+5
1

"errors" , : http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/view.html#view-jsp-formtaglib-errorstag. :

if(bindingResult.hasErrors()) {
   ... display your form page again 
   (and "errors" tags will be populated by validation messages corresponding to each field) ...
}
else {
   ... here, you know that your RegistrationData is valid so you can treat it (save it I guess) ...
}
+1

All Articles