Spring 3 @RequestMapping 应该自动绑定请求参数吗?
自动绑定请求参数似乎是默认行为,但我找不到很多关于它的文档。这是我的例子:
<form:form modelAttribute="test" action="testsubmit.do" method="POST">
Name: <form:input path="name" />
Nested Name: <form:input path="test.nestedName"/>
<input type="submit"/>
public class Test {
public String name;
public String name2;
public TestNested test;
...
public class TestNested {
public String nestedName;
...
现在使用我的映射:
@RequestMapping(value = "/testsubmit")
public String testSubmit(Test test){
...
test
对象正在绑定表单值,包括嵌套值。在我看来,这似乎是预期的行为,但我对 @ModelAttribute 注释及其与指定为映射方法参数的对象的使用有点困惑。
15.3.2.8使用 @ModelAttribute
提供模型数据的链接表示:
当您将其放在方法参数上时,
@ModelAttribute
会将模型属性映射到特定的带注释的方法参数(请参阅下面的processSubmit()
方法)。这就是控制器如何获取对保存表单中输入的数据的对象的引用。
当我在加载时将对象 test
绑定到表单时,我将一个值设置为 name2
。
@RequestMapping(value = "/test")
public String test(Model model) {
Test test = new Test();
test.setName2("test name2");
model.addAttribute("test", test);
return "test";
}
当我用 @ModelAttribute("test")
注释 test
参数时,这不会在 submit
方法上传递:
@RequestMapping(value = "/testsubmit")
public String testSubmit(@ModelAttribute("test") Test test) {
...
这是预期的对我来说,因为 name2
未指定为表单字段/请求参数,但这并不能帮助我理解 @ModelAttribute("test")
用法的要点。有人能为我解释一下吗?
Auto-binding of request params seems like default behaviour, but I can't find a lot of documentation on it. Here is my example:
<form:form modelAttribute="test" action="testsubmit.do" method="POST">
Name: <form:input path="name" />
Nested Name: <form:input path="test.nestedName"/>
<input type="submit"/>
public class Test {
public String name;
public String name2;
public TestNested test;
...
public class TestNested {
public String nestedName;
...
Now with my mapping:
@RequestMapping(value = "/testsubmit")
public String testSubmit(Test test){
...
The test
object is binding the form values including the nested value. This seems to me like expected behavior, but I am a bit confused by the @ModelAttribute
annotation and its use with respect to objects specified as mapped method parameters.
15.3.2.8 Providing a link to data from the model with @ModelAttribute
says:
When you place it on a method parameter,
@ModelAttribute
maps a model attribute to the specific, annotated method parameter (see theprocessSubmit()
method below). This is how the controller gets a reference to the object holding the data entered in the form.
When I bind the object test
to the form on load, I set a value to name2
.
@RequestMapping(value = "/test")
public String test(Model model) {
Test test = new Test();
test.setName2("test name2");
model.addAttribute("test", test);
return "test";
}
This doesn't get passed through on the submit
method when I annotate the test
parameter with @ModelAttribute("test")
:
@RequestMapping(value = "/testsubmit")
public String testSubmit(@ModelAttribute("test") Test test) {
...
This is expected to me as name2
was not specified as a form field/request param, but it doesn't help me understand the point of the @ModelAttribute("test")
usage. Can anyone shed some light on this for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@ModelAttribute 只是让您为模型中的对象指定不同的名称/键。如果您不使用它,Spring 将根据对象的类自动分配一个名称,例如,在“Test”类的情况下为“test”。使用@ModelAttribute,您可以通过指定“@ModelAttribute("whatever")”将模型中对象的名称/键更改为“whatever”。
@ModelAttribute just lets you specify a different name/key for your object in the model. If you don't use it, Spring will automatically assign a name based on the object's class, e.g. "test" in the case of your "Test" class. With @ModelAttribute you could change the name/key of your object in the model to, for example, "whatever" by specifying "@ModelAttribute("whatever")".