Java - Spring 3.0 MVC 和 @ModelAttribute

发布于 2024-12-16 10:54:53 字数 1619 浏览 4 评论 0原文

我需要一些关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身狗的梦 2024-12-23 10:54:53

这里是spring mvc参考。简单浏览了一下,发现有两种方法:

  1. @InitBinder
  2. @ModelAttribute("bean_name") 和方法。

您可以首先使用自定义数据绑定,从而动态创建命令对象。第二个允许您用它注释方法并使用此名称预填充模型属性:

@ModelAttribute("bean_name")
public Collection<PetType> populatePetTypes() {
    return this.clinic.getPetTypes();
} 

我希望它会使用名称“bean_name”填充模型属性(如果它为空)。

Here is the spring mvc reference. Looked through it briefly and found 2 approaches:

  1. @InitBinder
  2. @ModelAttribute("bean_name") with method.

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:

@ModelAttribute("bean_name")
public Collection<PetType> populatePetTypes() {
    return this.clinic.getPetTypes();
} 

I hope it will populate model attributes with name 'bean_name' if it is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文