Spring 3参考资料告诉我们:
When you place it on a method
parameter,@modelattribute
maps a
model attribute to the specific,
annotated method parameter
我不明白这个魔法咒语,因为我确定模型对象的别名(如果使用ModelMap作为返回类型的键值)在执行请求处理程序方法后传递给View.因此,当请求处理程序方法执行时,模型对象的名称无法映射到方法参数.
为了解决这个矛盾我去了stackoverflow并找到了this详细的例子.
例子的作者说:
// The “personAttribute” model has
been passed to the controller from the
JSP
看来,他被Spring引用所吸引……
为了消除魅力,我在我的环境中部署了他的示例应用程序,并从方法MainController.saveEdit()中残留地剪切了@modelattribute注释.结果应用程序无需任何更改即可运行!所以我的结论是:将Web表单的字段值传递给参数的字段不需要@modelattribute注释.然后我坚持这个问题:@modelattribute注释的意思是什么?如果唯一的意思是在View中为模型对象设置别名,那么为什么这种方式比将对象显式添加到ModelMap更好呢?