By default, Spring MVC throws an exception when errors occur during request binding. This usually not what we want – instead we should be presenting these errors to the user. We’re going to use a BindingResult by adding one as an argument to our controller method:
The BindingResult argument needs to be positioned right after our form backing object – it’s one of the rare cases where the order of the method arguments matters. Otherwise we’ll run into the following exception :
java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply:
public java.lang.String controller.EmployeeController.addEmployees(org.springframework.validation.BindingResult,entity.Employee,org.springframework.ui.Model,org.springframework.web.servlet.mvc.support.RedirectAttributes)
To correct the above exception, just change order of the parameter, where your binding result parameter has to be just after the ModelAttribute as below
Happy Programming...!!!
@RequestMapping(value="/addEmployee", method=RequestMethod.POST)
public String addEmployees(BindingResult result, @ModelAttribute("employee") Employee employee,
Model model, final RedirectAttributes redirectAttributes){
The BindingResult argument needs to be positioned right after our form backing object – it’s one of the rare cases where the order of the method arguments matters. Otherwise we’ll run into the following exception :
java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply:
public java.lang.String controller.EmployeeController.addEmployees(org.springframework.validation.BindingResult,entity.Employee,org.springframework.ui.Model,org.springframework.web.servlet.mvc.support.RedirectAttributes)
To correct the above exception, just change order of the parameter, where your binding result parameter has to be just after the ModelAttribute as below
@RequestMapping(value="/addEmployee", method=RequestMethod.POST)
public String addEmployees( @ModelAttribute("employee") Employee employee,
BindingResult result, Model model, final RedirectAttributes redirectAttributes){
Happy Programming...!!!