What does it mean to say "if (binding.hasErrors())" in Spring?
In this example, I don't understand what the BindingResult
is for and what it开发者_如何学JAVA 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";
}
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.
精彩评论