[英]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. 我收到一个错误: Neither BindingResult nor plain target object for bean name 'foo' available as request attribute
都不能Neither BindingResult nor plain target object for bean name 'foo' available as request attribute
,我不知道为什么。 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: 在不起作用的JSP中:
<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: 在表示JSP的Java控制器中:
@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. foo
不会显示在配置器页面的其他任何地方。
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. 更新:这是FOOBEAN类。 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
. 此外, FOOBEAN
和Foo
不相关,只是Foo
是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: 您需要添加BindingResult对象作为参数,并按如下所示初始化模型:
@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. 请注意,必须在GET处理程序中调用getFoo方法,即用于返回视图名称的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.