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";
}
//...
}
Code language: Java (java)
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)
Code language: Java (java)
What!!!?
How to solve below exception in Spring MVC?
java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature.
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)
Code language: Java (java)
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.
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
I had the same issue too! But I've read your blog after finding this by myself. But good article!
[code language="java"]
public String saveCompanyEventsBasicInfo(final CompanyEvents companyEvents,
final BindingResult bindingResult, final ModelMap model) {
validateEventImage(companyEvents, bindingResult);
if (bindingResult.hasErrors()) {
return "leave/leavemaster";
}
return null;
}
[/code]
sry.same error occurs .
I too was having the same error. Thanks this post help me understand the error.
its working....thanks
Excellent, Thanks very much for this blog. I had the same issue wasting my time and then ended up on this blog and solution worked like a magic.... Thanks again
Very very useful...
Thanks for the post
ResponseEntity submitPetData(@RequestHeader(value = "Authorization", required = true) String token, @Valid @PathVariable(value = "countryCode", required = true) String countryCode,
BindingResult bindingResult, Principal principal) {
For my example I dont have modelAttribute.. I am using @PathVariable and geting the same error
Caused by: 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 org.springframework.http.ResponseEntity