aspose file tools*
The moose likes Spring and the fly likes Neither BindingResult nor plain target object for bean name 'command' available as request attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Neither BindingResult nor plain target object for bean name Watch "Neither BindingResult nor plain target object for bean name New topic
Author

Neither BindingResult nor plain target object for bean name 'command' available as request attribute

Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 106
I'm returning to a problem I posted on previously, so apologies in advance. I've gotten some better understanding of the basics of Spring MVC, so I'm in better shape, but I guess I'm still confused. I'm getting the error referred to in the subject line. Here is the form I'm calling my controller code from. This is literally all it is. There are no fields to be submitted, so I'm not passing anything to the controller. There are probably better ways of calling the controller than an input button, but that's not the problem I need help with here.



Here is my controller. I'm trying to give it something for the model. I just don't know what to give it. I'm putting a List<Bulletin> in the ModelAndView. I have verified that this method does run in its entirety. I just get the error message upon completion.


For the record, here is the root cause in the error log, which may be helpful. Something is obviously not being bound, but I'm not sure what I'm supposed to be binding, what I'm supposed to be binding it to, and when I'm supposed to be doing this binding.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Typically there would be a GET which would populate what you need for the page. The Spring form tag has a modelAttribute attribute which specifies the name of the object that backs the form and that the elements of the form bind to. When you submit the form this is the object passed into your controller and the binding result pertains to it. Have a look at this example.

http://krams915.blogspot.com/2011/01/spring-mvc-3-hibernate-annotations.html

notice the editPage.jsp and the form tag and how the path attributes bind inputs to the form backing object.

[How To Ask Questions][Read before you PM me]
Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 106
Bill Gorder wrote:Typically there would be a GET which would populate what you need for the page. The Spring form tag has a modelAttribute attribute which specifies the name of the object that backs the form and that the elements of the form bind to. When you submit the form this is the object passed into your controller and the binding result pertains to it. Have a look at this example.

http://krams915.blogspot.com/2011/01/spring-mvc-3-hibernate-annotations.html

notice the editPage.jsp and the form tag and how the path attributes bind inputs to the form backing object.


The form in question is on the front page of the application, so there isn't really an object that backs it. There is, however, a controller for the front page. I'm wondering if I should put an object in the model in the front page controller. Also, the method being called by the form in question returns a List<Bulletin>, not a Bulletin. How does this fact relate to the modelAttribute?
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 46
You are correct you need to put a form backing object in the model inside the method which is handling GET request.what happens is when modelandview object is returned from requestHandler method in the view layer there is a method which retrives the formbacking object from Model or modelMap and set in request.

so in form you need to use a modelAttribute inside form tag and whose value would be by the name you put your object in model.

nor plain target object for bean name 'command' available as request attribute
by this line in exception means even if you are not defining any
modelAttribute inside form tag then conventionally it looks for a bean nammed "command" in request scope and if it finds one it treats that as form backing object.

once you post the form the same form backing object is set in request in the name of @ModelAttribute("name") typically for view layer display.
Also, the method being called by the form in question returns a List<Bulletin>, not a Bulletin. How does this fact relate to the modelAttribute?


this is meant for view layer put it in model and display.I think it doesn't not have any connection with form modelAttribute.

Regards

Ayan


Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 106
I have replaced the form on the front page with a link whose code looks like this:



Is there any way to pass the form back object through this link, or should I revert back to the form-based approach and pass the object through the form?
Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 106
I've had an idea. This is the entire controller for my front page.



Should I add a Bulletin object to the model in this controller by doing something like this?
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 46
yes you are correct.you need to add a Bulletin object to the model like what you have shown. and you need to add modelAttribute=form backing object key in model.



or if you give key name as "command" then conventially you don't have to declare modelAttribute inside form tag.like below

model.addAttribute("bulletin", new Bulletin());

in this case if you leave your form in earlier state it will work also.

Regards

Ayan
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 46
or if you give key name as "command" then conventially you don't have to declare modelAttribute inside form tag.like below

model.addAttribute("bulletin", new Bulletin());


sorry it would be actually

model.addAttribute("command", new Bulletin());
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 46
Frank Serkland wrote:I have replaced the form on the front page with a link whose code looks like this:



Is there any way to pass the form back object through this link, or should I revert back to the form-based approach and pass the object through the form?



since your form not having any field so link will also do just make sure you change method type inside controller from POST to GET.because when you click link one GET type request would be sent.


Regards

Ayan
Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 106
I added this command to my controller, and although I'm having another problem now, this line resolved the problem in this thread.


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Neither BindingResult nor plain target object for bean name 'command' available as request attribute