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";
    }
}
<!--formatted-->

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";
    }
}
<!--formatted-->

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.

11 Comments

  1. Oleksii said:

    Hey dude! Nice job! I just spent at least 2 hours to solve this problem before found this blog. Thanks a lot!

    February 22, 2015
    Reply
    • Bo Andersen said:

      You are welcome. I am happy that you solved the problem! :-)

      March 18, 2015
      Reply
  2. Rukundo said:

    Thank you guy i was looking fot 2 days to solve the problem but you save my time Thank you very much

    March 18, 2015
    Reply
    • Bo Andersen said:

      You are very welcome, Rukundo. I am happy to hear that you solved the problem. :-)

      March 18, 2015
      Reply
  3. Ken said:

    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!

    August 10, 2015
    Reply
    • Bo Andersen said:

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

      August 10, 2015
      Reply
  4. Arpan said:

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

    January 11, 2016
    Reply
  5. Vladimir said:

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

    January 14, 2016
    Reply
  6. Bouba said:

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

    April 11, 2016
    Reply
  7. Venkata said:

    I tried above procedure still i am getting same error.

    July 5, 2016
    Reply
  8. Roger said:

    Hi,BO!

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

    December 1, 2016
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *