Neither BindingResult nor plain target object available as request attribute
If you are trying to bind a Spring MVC form to a bean, then you might have come across the below exception.
Neither BindingResult nor plain target object for bean name ‘mybean’ available as request attribute
Some tutorials claim that you can do like this:
@Controller
public class PersonController {
@RequestMapping(value = "/person/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("person", new Person());
return "add-person";
}
@RequestMapping(value = "/person/add", method = RequestMethod.POST)
public String processAdd(@Valid Person person, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "add-person";
}
return "person-added-successfully";
}
}
The above assumes a Spring form with a modelAttribute or commandName attribute with the value person. This code does, however, raise the following exception: Neither BindingResult nor plain target object for bean name ‘mybean’ available as request attribute.
This used to work in previous versions of Spring, but it no longer seems to work (currently version 4.1.4). To solve this, you have to add a @ModelAttribute annotation before your bean argument in the controller action with the POST request method. Below is a working example.
@Controller
public class PersonController {
@RequestMapping(value = "/person/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("person", new Person());
return "add-person";
}
@RequestMapping(value = "/person/add", method = RequestMethod.POST)
public String processAdd(@Valid @ModelAttribute("person") Person person, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "add-person";
}
return "person-added-successfully";
}
}
And the corresponding view:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<c:url var="actionUrl" value="/person/add" />
<form:form action="${actionUrl}" modelAttribute="person" method="POST" acceptCharset="UTF-8">
<form:input path="firstName" />
<form:errors path="firstName" />
<form:input path="lastName" />
<form:errors path="lastName" />
<form:button id="add-person">Add Person</form:button>
</form:form>
The Person class is just a simple bean like the below.
public class Person {
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
}
Note that I am using JSR-303 bean validation with the Hibernate Validator implementation. If you do not, then you can simply remove the annotations from the bean class as well as the @Valid annotation in the arguments of the processAdd controller action.
That is all there is to it. At least it worked for me, and hopefully it will work for you too.
21 comments on »Neither BindingResult nor plain target object available as request attribute«
Hey dude! Nice job! I just spent at least 2 hours to solve this problem before found this blog. Thanks a lot!
You are welcome. I am happy that you solved the problem! :-)
Thank you guy i was looking fot 2 days to solve the problem but you save my time Thank you very much
You are very welcome, Rukundo. I am happy to hear that you solved the problem. :-)
Hi, Bo! I spent at least an hour to resolve this issue. And there is no suggestion in official spring documentation. You saved my day. Thanks a lot for this publication!
Yeah, it was also a tricky one for me. Since I had not found any solutions to the problem online, I decided to write one up to help others. I am happy that it was useful for you! :-)
Thanks a ton! I spent 3 hours and no other article except this is available.
Cheers!
Thank you! You have really helped me. Good luck!!!
Thank you, I’m happy with your help!!!
I tried above procedure still i am getting same error.
Hi,BO!
Could you mind share this example-code to me?
(I have spend 2-days on this issue….)
Hi,
I have similar problem. Kindly have a look at the code below:
My view looks like this :
My Controller looks like this:
Even after making the changes as you suggested in your blog, I am still getting the error message ‘java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘user’ available as request attribute’
Kindly guide me to overcome this error.
Thanks in advance.
Kind regards,
Satheesh
Thank you!
So this error only occurs because of the spring versions? If so, hwta was changed from the older spring compared to 4.x.x…
Sir Thanks a lot you really saved me..! God Bless you
Thanks a lot for this! You litterally saved me a lot of time of browisng!
Thank you very much! I’ve been searching for hours for the reason for that hour to no avail.
you’re surely going in heaven for this article bro.
It is an old article but this very useful! I’ve been struggling with this issue for hours :D.. thanks!
This fixed my issue right away after getting these errors updating an old application from Spring 4 to 5.
Truly a god amongst men.
Excellent work whoever is, this is a great help, appreciate brother.
Kudos to you.
This solved my problem too.