简体   繁体   中英

Neither BindingResult nor plain target object for bean name available as request attribute, and I don't know why

I'm getting an error: Neither BindingResult nor plain target object for bean name 'foo' available as request attribute and I can't figure out why. I know there are some questions regarding this already, and I have tried their solutions to no avail.

As far as the headers for the code I have (all actual variables have been replaced with metasyntactic variables):

In the JSP that doesn't work:

<form:form name="myForm"
class="form-horizontal form-row-seperated" action="#"
onsubmit="return isformSubmit();" method="post" 
modelAttribute="foo">

In the Java controller for said JSP:

@RequestMapping(value="bar", method = RequestMethod.POST)
public @ResponseBody Boolean baz(
    @ModelAttribute("foo") FOOBEAN fooBean,
// Ten @RequestParam strings have been omitted for brevity
 , HttpServletRequest qux) throws QUUXEXCEPTION {

foo does not show up anywhere else in the configurator page.

I know I'm doing something wrong, but I don't know what exactly. If I'm not giving enough context, please let me know.

UPDATE: Here's the FOOBEAN class. I'm reusing metasyntactic variables here; they are not the same variables that were in the controller class. Also, FOOBEAN and Foo are not related other than that Foo is a variable within FOOBEAN .

public class FOOBEAN {

     private String Foo;

     private String Bar;

     private String Baz;

     private String Qux;

     private String Quux;

     private String Corge;

     private String Grault;

     private String Garply;


     public String getFoo() {
        return Foo;
    }

    public void setFoo(String foo) {
        Foo = foo;
    }

    public String getBar() {
        return Bar;
    }

    public void setBar(String bar) {
        Bar = bar;
    }

    public String getBaz() {
        return Baz;
    }

    public void setBaz(String baz) {
        Baz = baz;
    }

    public String getQux() {
        return Qux;
    }

    public void setQux(String qux) {
        Qux = qux;
    }

    public String getQuux() {
        return Quux;
    }

    public void setQuux(String quux) {
        Quux = quux;
    }

    public String getCorge() {
        return Corge;
    }

    public void setCorge(String corge) {
        Corge = corge;
    }

    public String getGrault() {
        return Grault;
    }

    public void setGrault(String grault) {
        Grault = grault;
    }

    public String getGarply() {
        return Garply;
    }

    public void setGarply(String garply) {
        Garply = garply;
    }

}

You need to add the BindingResult object as a parameter and initialise the model as follows:

@ModelAttribute("foo") 
public FOOBEAN getFoo(){ 
    return new FOOBEAN();
}

public @ResponseBody Boolean baz(@ModelAttribute("foo") FOOBEAN fooBean, BindingResult result, HttpServletRequest qui) {
    // method body does here 
}

Note that the getFoo method must be called in the GET handler, ie the method you use to return the view name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM