Neither BindingResult nor plain target object available as request attribute

Published on February 21, 2015 by

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.

Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

21 comments on »Neither BindingResult nor plain target object available as request attribute«

  1. Oleksii

    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! :-)

  2. Rukundo

    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. :-)

  3. Ken

    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! :-)

  4. Arpan

    Thanks a ton! I spent 3 hours and no other article except this is available.
    Cheers!

  5. Vladimir

    Thank you! You have really helped me. Good luck!!!

  6. Bouba

    Thank you, I’m happy with your help!!!

  7. Venkata

    I tried above procedure still i am getting same error.

  8. Roger

    Hi,BO!

    Could you mind share this example-code to me?
    (I have spend 2-days on this issue….)

  9. Satheesh CK

    Hi,
    I have similar problem. Kindly have a look at the code below:
    My view looks like this :

    My Controller looks like this:

    
    @RequestMapping("/register")
    	public String showRegister(Model model) {
    		model.addAttribute("user", new User());
    		return "register";
    	}
    
    	///@RequestMapping("/registeruser")
    	@RequestMapping(value = "/registeruser", method = RequestMethod.POST)
    	public String showRegisterUser(Model model, @Valid @ModelAttribute("user") User user, BindingResult result) {
    		if (result.hasErrors()) {
    
    			return "register";
    		}
    
    	}
    
    

    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

  10. Martin

    Thank you!

  11. Orlando

    So this error only occurs because of the spring versions? If so, hwta was changed from the older spring compared to 4.x.x…

  12. gervais

    Sir Thanks a lot you really saved me..! God Bless you

  13. Boris

    Thanks a lot for this! You litterally saved me a lot of time of browisng!

  14. Deadlock3563

    Thank you very much! I’ve been searching for hours for the reason for that hour to no avail.

  15. hb210

    you’re surely going in heaven for this article bro.

  16. Zoltan

    It is an old article but this very useful! I’ve been struggling with this issue for hours :D.. thanks!

  17. Daniel

    This fixed my issue right away after getting these errors updating an old application from Spring 4 to 5.

    Truly a god amongst men.

  18. Sumit

    Excellent work whoever is, this is a great help, appreciate brother.
    Kudos to you.
    This solved my problem too.

Leave a Reply

Your e-mail address will not be published.