Solve:Errors/BindingResult argument declared without preceding model attribute
- By Viral Patel on November 7, 2012
Sometimes Spring MVC will amaze you with totally unexpected exceptions. You have no idea why that is coming.
For instance, recently I wrote a small piece of Spring MVC Controller code, one like below:
@Controller
public class UserController {
@RequestMapping(value = "addUser")
public String addUser(@ModelAttribute("userForm") UserForm userForm,
ModelMap map, BindingResult results) {
if (results.hasErrors()) {
return "add_user_form";
}
return "add_user_success";
}
//...
}
And while executing the application, I got a strange exception:
Throwable occurred: java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature! at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:335) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
What!!!?
Problem Statement
How to solve below exception in Spring MVC?
java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature.
Solution
Well basically Spring expects the BindingResult attribute to be present right after your Model attribute. So if we change the above controller method to:
@RequestMapping(value = "addUser")
public String addUser(@ModelAttribute("userForm") UserForm userForm,
BindingResult results, ModelMap map)
This will solve the error. Just keep in mind. Whenever you want to define BindingResult object in your controller method, declare it right after the (@ModelAttribute) model attribute.
Spring’s documentation says:
The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more that one model object and Spring will create a separate BindingResult instance for each of them so the following sample won’t work:
Hope this is useful.
Get our Articles via Email. Enter your email address.
I had the same issue too! But I’ve read your blog after finding this by myself. But good article!