Spring MVC – Neither BindingResult nor plain target object for bean name ‘xxx’ available as request attribute.

Problem

Recently, just converted the Spring MVC xml-based form controller to annotation-based form controller, and hits the following error message.

SEVERE: Neither BindingResult nor plain target object for bean name ‘customerForm’ available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘customerForm’ available as request attribute


Above error message is clearly indicated that the “customerForm” bean is not exists, and i 100% sure the view resolver is configured properly and the “CustomerForm.jsp” view page is existed.

Form Controller


@Controller
@RequestMapping("/customer.htm")
public class CustomerController{

       @RequestMapping(method = RequestMethod.GET)
	public String initForm(ModelMap model){
		//return form view
		return "CustomerForm";
	}

View Resolver


         ...
	<bean id="viewResolver"
	      class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
              <property name="prefix">
                  <value>/WEB-INF/pages/</value>
              </property>
              <property name="suffix">
                 <value>.jsp</value>
             </property>
        </bean>

Solution

The root caused is the incorrect view name in JSP page, see below.


<form:form method="POST" commandName="customerForm">

The “customerForm” is not exists in the controller mapping anymore, see annotation mapping @RequestMapping(“/customer.htm”), it should change to “customer”.


<form:form method="POST" commandName="customer">

Similar Cases

I’ve seen quite many similar cases happened in validator or SimpleFormController class as well. To solve it, just make sure the mapping name is matched or existed.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Mikkel
Guest
Mikkel

Thank you mkyoung! :) I Just had a similar problem and this helped me out.

Mikkel
Guest
Mikkel

BTW are you sure that it is because of @RequestMapping(“/customer.htm”) that it should be: . It would be much simpler if it was a mapping to @ModelAttribute(“customer”) for example.

I am not sure which one it is, since in my case the names are the same.

Mikkel
Guest
Mikkel

After some tests I am sure that it is commandName=”customer” because of @ModelAttribute(“customer”) and not @RequestMapping(“/customer.htm”).

But still thanks since you lead me in the right direction with the commandName attribute. And then also because of all your nice tutorials, which I have learned alot from in the last 6 month. The date I found it :)

arun singh
Guest
arun singh

this exception occurs when the spring container does not find “customerForm” named key in Model-map in the Session of Controller. The form data are considered as “value” and “customerForm” is “key”.
To resolve it, you should keep a “Customer” object ready so that given “commandName”‘s value can be saved into the object first.
Solution= model.addAttribute(“customerForm”, new Customer()); //when the request comes for the first time, you should attach a Customer object to the Model, means you are making target object available for the commandName.

Avril Sioson-Miranda
Guest
Avril Sioson-Miranda

Hi,

Thanks for pointing this out :)

Ajeet Deginal
Guest
Ajeet Deginal
My controller: @RequestMapping(method=RequestMethod.POST, value=”/searchCompanyDetails”) public String searchCompanyDetails(ModelMap model, @ModelAttribute(“company”) CompanyDetails company, HttpServletRequest request){ model.put(“companyName”, company.getCompanyName()); model.put(“contact”, new ContactInformationDTO()); List categoryList = services.getCategryList(); model.put(“categorylist”, categoryList); model.put(“comcat”, new CategoryCompanyDTO()); return “addCategory2”; } My JSP: <%– –%> Error: SEVERE: Servlet.service() for servlet [ConceptEdgeDetails] in context with path [/ConceptEdge] threw exception [An exception occurred processing JSP page /WEB-INF/views/addCategory2.jsp at line 55 java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘contact’ available as request attribute Please help me where I am doing wrong?
Narendra
Guest
Narendra

Neither BindingResult nor plain target object for bean name ‘xxx’ available as request attribute

I am using SimpleFormController not RequestMapping Annotaion.could u plz help me in resolving the issue.
Thanks

Shekar Chandra
Guest
Shekar Chandra

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘loginform’ available as request attribute

Could someone please help me out here

Mounir Igorman
Guest
Mounir Igorman

i have the same probleme and i can”t resolve it yet !!
this is my controller
@RequestMapping(value=”/addProduct”, method = RequestMethod.POST)
public String addProduct(Model model,@ModelAttribute(“produit”) Produit produit,BindingResult result){
produitValidator.validate(produit,result);
if (!result.hasErrors()) {
services.addProduit(produit);
model.addAttribute(“produit”,new Produit());
model.addAttribute(“listeProduit”, services.getAllProduits());
return “produits”;
}else return “error”;
}

this is my jsp

nom:

Prabhat Singh
Guest
Prabhat Singh

Thanks helped me

AVA
Guest
AVA

Add Model Attribute as follows to resolve the issue:

@ModelAttribute(“customer”)
public Customer getCustomerObject() {
return new Customer();
}

read posts :
(1) http://stackoverflow.com/questions/8781558/neither-bindingresult-nor-plain-target-object-for-bean-name-available-as-request
(2) http://stackoverflow.com/questions/1069958/neither-bindingresult-nor-plain-target-object-for-bean-name-available-as-request

gelete
Guest
gelete

Thank u, adding this new annotated method resolves my problem

puneet kumar agarwal
Guest
puneet kumar agarwal

the post has been very informative and helped to find the issue. save my day. thanks and keep posting :)

ybaburao
Guest
ybaburao
I am a new to Spring 2.5 MVC, and I need some help. I must be missing something, but I can’t figure out what. It is giving java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘user’ available as request attribute , when i clicked on MyLogin url. Here Iam trying to using UrlFilenameViewController and SimpleFormController. index.jsp: ———- UrlFileNameViewController Example My Login My Office Address Iam using UrlFilenameViewController to display this page dispatcher-servelt.xml: ————————— /WEB-INF/jsp/ .jsp true user com.model.User when i run index.jsp , it will display MyLogin and MyOffice Login when i click on MyLogin Link in getting… Read more »
Carol
Guest
Carol

Thank you for any other great article. The place else may just anybody get that type of information in such a perfect manner of writing?

I’ve a presentation subsequent week, and I’m at the look for such information.

Christian
Guest
Christian

This could also happen if you are passing a class object with a null value into the ModelAndView. In which case you must at least instantiate a new instance of the class.

Shabs
Guest
Shabs

Thanks Mkyong,

Many of your posting have been of great help to me. Really appreciate and admire your good work. Thanks again.

Shabs

alle
Guest
alle

“Neither BindingResult nor plain target object for bean name ‘xxx’ available as request attribute
one of the root causes for this exception is.
we must set the object instance in request of jsp page.
eg:

Sunil
Guest
Sunil

Thanks alle your comment helped me.

Lenny
Guest
Lenny

I also had the same problem as “Neither BindingResult nor plain target object for bean name…..”, but my problem was that in my jsp I was using <form…. instead of <form:form….. since I was using the form taglib, I needed to use <form:form. This solved the problem for me.

jaime
Guest
jaime

Thank you, I was confusing the ‘commandName’ attribute

Nagendra Gupta
Guest
Nagendra Gupta

Did the same as you tole bust still problem coming??