views:

19

answers:

1

In this example, I don't understand what the BindingResult is for and what it means to say if (binding.hasErrors()) below.

@RequestMapping(value = "/test", method = RequestMethod.POST)
public final String submit(@ModelAttribute(TEST) @Valid final Test test, final BindingResult binding,
    final HttpServletRequest request, final ModelMap modelMap)
{

    if (binding.hasErrors())
    {
        return "test";
    }
+2  A: 

BindingResult is a data binding result associated with the previous argument (that is, test). It holds information about the errors of binding request parameters to the properties of test, such as type mismatches. When @Valid annotation is present, it also holds errors produced by the authomatic validation of test.

So, binding.hasErrors() determines whether errors was found during binding and validation of the test. When such errors present, the typical behaviour is to redisplay the form with errors messages.

axtavt
Great explanation. Thank you.
Gary Ford