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.

Featured

Learn Spring Framework today!

Take an online course and become a Spring Framework champion!

Here is what is covered:

  • The Spring Framework basics, including Spring MVC
  • How to work with JDBC and MySQL (in the context of Spring)
  • Build real applications step by step
  • A detailed walkthrough of Spring's core concepts
  • ... and much more!
Spring Framework logo
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!

12 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";
    	}
    
    	[email protected]("/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

Leave a Reply

Your e-mail address will not be published.