Java - Spring 3.0 MVC 和 @ModelAttribute
我需要一些关于 Spring 3.0 MVC 和 @ModelAttribute 带注释的方法参数的澄清。我有一个看起来像这样的控制器:
RequestMapping(value = "/home")
@Controller
public class MyController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView foo() {
// do something
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView bar(
@ModelAttribute("barCommand") SomeObject obj) {
// do sometihng with obj and data sent from the form
}
}
在我的 home.jsp 上,我有一个像这样的表单,它将他的数据发送到 MyController 的 RequestMethod.POST 方法
<form:form action="home" commandName="barCommband">
</form:form
现在,如果我尝试访问 home.jsp,我会收到此异常
java.lang.IllegalStateException:
Neither BindingResult nor plain target object for bean name 'barCommand' available as request attribute
:解决这个问题我发现我需要将
@ModelAttribute("barCommand") SomeObject obj
参数添加到 MyController 的 Request.GET 方法中,即使我不会在该方法中使用 obj 。例如,如果使用不同的 commandName 将另一个表单添加到 home.jsp,如下所示:
<form:form action="home/doSomething" commandName="anotherCommand">
</form:form
我还必须在 RequestMethod.GET 上添加该参数,现在看起来像:
@RequestMapping(method = RequestMethod.GET)
public ModelAndView foo( @ModelAttribute("barCommand") SomeObject obj1,
@ModelAttribute("anotherCommand") AnotherObj obj2) {
// do something
}
或者我得到相同的异常。我要问的是,这是否是正常的 Spring 3 MVC 行为,或者我是否做错了什么。为什么我需要将所有 @ModelAttribute 参数放在 RequestMethod.GET 方法上?
预先感谢您帮助
斯特凡诺
I need some clarification about Spring 3.0 MVC and @ModelAttribute annotated method parameter. I have a Controller which looks like this one:
RequestMapping(value = "/home")
@Controller
public class MyController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView foo() {
// do something
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView bar(
@ModelAttribute("barCommand") SomeObject obj) {
// do sometihng with obj and data sent from the form
}
}
and on my home.jsp i have a form like this one which sends his data to the RequestMethod.POST method of MyController
<form:form action="home" commandName="barCommband">
</form:form
Now if i try to access home.jsp i get this Exception:
java.lang.IllegalStateException:
Neither BindingResult nor plain target object for bean name 'barCommand' available as request attribute
To resolve this i found that i need to add the
@ModelAttribute("barCommand") SomeObject obj
parameter to the Request.GET method of MyController, even if i won't use obj in that method. And for example if add another form to home.jsp with a different commandName like this:
<form:form action="home/doSomething" commandName="anotherCommand">
</form:form
i also have to add that parameter on the RequestMethod.GET, which will now look like:
@RequestMapping(method = RequestMethod.GET)
public ModelAndView foo( @ModelAttribute("barCommand") SomeObject obj1,
@ModelAttribute("anotherCommand") AnotherObj obj2) {
// do something
}
or i get the same exception. What i'm asking is if this is a normal Spring 3 MVC behaviour or if i'm doing something wrong. And why do i need to put all the @ModelAttribute parameters on the RequestMethod.GET method?
Thanks in advance for you help
Stefano
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里是spring mvc参考。简单浏览了一下,发现有两种方法:
您可以首先使用自定义数据绑定,从而动态创建命令对象。第二个允许您用它注释方法并使用此名称预填充模型属性:
我希望它会使用名称“bean_name”填充模型属性(如果它为空)。
Here is the spring mvc reference. Looked through it briefly and found 2 approaches:
You may use first to customize data binding and, thus, create command objects on-the-fly. Second allows you to annotate method with it and prepopulate model attributes with this name:
I hope it will populate model attributes with name 'bean_name' if it is null.