19 April 2011

how to solve error Neither BindingResult nor plain target object for bean name '' available as request attributeeweeeeeee

Solution for Spring 3.0.x

If you see error like this: "Neither BindingResult nor plain target object for bean name ... available as request attribute "  and you get mad , then instead of punch innocent display ... try to chill out, drink beer and look to your code more carefully,because above error mean that have "a little" bit mess in your form(model) java file or jsp file


Probably:
1) in form(model) file:

a)you have not implement setters , getters or both
b) they are named wrongly(misspelled?) (so spring couldn't find it)

package xxx.losers.epicfail.forms.FailForm;

public class FailForm {

String name;
String description;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}

}


2) in your jsp file
in your form, path attribute is misspell or doesn't exist in form

<form:form name=" failform" method="POST" commandName=" failform " action="uploadfailform.html"> <fieldset>
<legend>Upload Image</legend>
<p>
  <form:input  path="name" />
    <form:input  path="description" />
</p>

</fieldset>
</form:form>



<form:form action="/securedArea" commandName="uploadItem" enctype="multipart/form-data"></form:form>

3)Check is your element is between <form> </form>

<form:form name=" failform" method="POST" commandName=" failform " action="uploadfailform.html"> <fieldset>
<legend>Upload Image</legend>
<p>
  <form:input  path="name" />
    <form:input  path="description" />
</p>
</fieldset>

</form:form>
4)

In your controller you added @ModelAttribute(" failform ") FailForm  failform  add failform to model
public String create(HttpSession session, @ModelAttribute(" failform ") FailForm  failform , BindingResult result, Model model) {
 (...)
  model.addAttribute("failform ",  failform ); 
  return  "fail/epic";


5 and more?) Probably other reasons exist,but i didn't come across ... yet.