Java @ModelAttribute 注释参数的数据绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/909396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data binding of @ModelAttribute annotated parameters
提问by Jaap Coomans
I have a Spring 2.5 annotated Controller in which I have a method annotated with @RequestMapping(method=RequestMethod.GET), which performs some logic to fill the model.
我有一个 Spring 2.5 注释控制器,其中我有一个用 @RequestMapping(method=RequestMethod.GET) 注释的方法,它执行一些逻辑来填充模型。
I also have a method annotated with @RequestMapping(method=RequestMethod.POST) which performs the request. This method has a @ModelAttribute annotated parameter that contains my own form pojo, let's call it MyForm. I also have an initialization method for MyForm, also annotated with @ModelAttrribute. Now so far all works as expected: on a POST request the form data binds to MyForm and I can process it.
我还有一个用 @RequestMapping(method=RequestMethod.POST) 注释的方法,它执行请求。这个方法有一个@ModelAttribute 注释参数,它包含我自己的表单pojo,我们称之为MyForm。我还有一个 MyForm 的初始化方法,也用@ModelAttrribute 进行了注释。现在到目前为止一切都按预期工作:在 POST 请求中,表单数据绑定到 MyForm,我可以处理它。
The problem is that I want to be able to pre-populate the form by passing in (GET) request parameters. Since I have the @ModelAttribute method for MyForm, I do get a MyForm instance in my model, but it doesn't get populated unless I specifically use it as a parameter for my GET method.
问题是我希望能够通过传入 (GET) 请求参数来预填充表单。因为我有 MyForm 的 @ModelAttribute 方法,所以我在我的模型中得到了一个 MyForm 实例,但它不会被填充,除非我专门将它用作我的 GET 方法的参数。
Why do I have to do this, is it possible to force data binding on my form for a GET request in a different way? I now just pass in the parameter, but because it is already in the model I don't have to do anything with it, resulting in an ugly unused method parameter.
为什么我必须这样做,是否可以以不同的方式在我的表单上强制对 GET 请求进行数据绑定?我现在只是传入参数,但因为它已经在模型中,所以我不必对它做任何事情,从而导致一个丑陋的未使用的方法参数。
[Edit: some code examples to illustrate]
[编辑:一些代码示例来说明]
The controller that doesn't populate the form on a get request:
在 get 请求中不填充表单的控制器:
@Controller
public class MyController {
@ModelAttribute("myForm")
public MyForm createForm() {
return new MyForm();
}
@RequestMapping(method=RequestMethod.GET)
public void handlePage(Model model) {
//Do some stuff to populate the model....
}
@RequestMapping(method=RequestMethod.POST)
public void processForm(@ModelAttribute("myForm") MyForm myForm) {
//Process the form
}
}
When I change the method signature of the handlePage method, it gets populated on a get request...
当我更改 handlePage 方法的方法签名时,它会在 get 请求中填充...
@RequestMapping(method=RequestMethod.GET)
public void handlePage(Model model, @ModelAttribute("myForm") MyForm myForm) {
//Do some stuff to populate the model....
}
回答by Corey
The method with the @ModelAttribute
is allowed to have any arguments that @RequestMapping
supports, so for example you could add how ever many @RequestParam
arguments as needed to populate your command object, or even the http request itself. I'm not sure if you can get an instance of the data binder in that same manner or not.
带有 的@ModelAttribute
方法可以有任何@RequestMapping
支持的参数,例如,您可以@RequestParam
根据需要添加多少个参数来填充命令对象,甚至是 http 请求本身。我不确定您是否可以以相同的方式获得数据绑定器的实例。
Reading the docs again I think the idea is that the pre-population in the @ModelAttribute
method would be database driven, which is probably why there isn't any data binding happening without adding the @ModelAttribute
as an argument to the @RequestMapping
method.
再次阅读文档,我认为这个想法是方法中的预填充@ModelAttribute
将是数据库驱动的,这可能就是为什么没有任何数据绑定发生的原因,而不是将 the@ModelAttribute
作为参数添加到@RequestMapping
方法中。