@ModelAttribute 注释在方法参数级别的含义是什么? [英] What is the meaning of @ModelAttribute annotation at method argument level?

查看:42
本文介绍了@ModelAttribute 注释在方法参数级别的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 3 参考教给我们:

Spring 3 reference teaches us:

当你把它放在一个方法上时参数,@ModelAttribute 映射一个模型属性到特定的,带注释的方法参数

When you place it on a method parameter, @ModelAttribute maps a model attribute to the specific, annotated method parameter

我不明白这个魔法咒语,因为我确定模型对象的别名(如果使用 ModelMap 作为返回类型,则为键值)在执行请求处理程序方法后传递给视图.因此,当请求处理程序方法执行时,模型对象的名称不能映射到方法参数.

I don't understand this magic spell, because i sure that model object's alias (key value if using ModelMap as return type) passed to the View after executing of the request handler method. Therefore when request handler method executes the model object's name can't be mapped to the method parameter.

为了解决这个矛盾,我去 stackoverflow 找到了 这个 详细的例子.例子的作者说:

To solve this contradiction i went to stackoverflow and found this detailed example. The author of example said:

//personAttribute"模型有已从控制器传递给控制器JSP

// The "personAttribute" model has been passed to the controller from the JSP

看来,他是被Spring引用迷住了……

It seems, he is charmed by Spring reference...

为了消除魅力,我在我的环境中部署了他的示例应用程序,并从方法 MainController.saveEdit() 中残忍地删除了 @ModelAttribute 注释.因此,该应用程序无需任何更改即可运行!所以我得出结论:不需要 @ModelAttribute 注释来将 Web 表单的字段值传递给参数的字段.然后我坚持了一个问题:@ModelAttribute 注释的含义是什么?如果唯一的方法是在 View 中为模型对象设置别名,那么为什么这种方式比将对象显式添加到 ModelMap 更好?

To dispel the charms i deployed his sample app in my environment and cruelly cut @ModelAttribute annotation from method MainController.saveEdit(). As result the application works without any changes! So i conclude: the @ModelAttribute annotation is not needed to pass web form's field values to the argument's fields. Then i stuck to the question: what is the mean of @ModelAttribute annotation? If the only mean is to set alias for model object in View, then why this way better than explicitly adding of object to ModelMap?

推荐答案

重点是 @ModelAttribute 是可选的 - 如果参数没有被注释并且它的类型没有特殊含义(即它不是 HttpServletRequestModelMap 等),它被当作 @ModelAttribute-annotated 参数对待.

The point is that @ModelAttribute is optional - if argument is not annotated and its type has no special meaning (i.e. it's not HttpServletRequest, ModelMap and so on), it's treated like @ModelAttribute-annotated argument.

所以,@ModelAttribute 在两种情况下是有效的:

So, @ModelAttribute is effectively needed in two cases:

  • 指定属性的名称.如果省略 @ModelAttribute 或具有空的 value,则使用默认名称(参数的类型名称,首字母去大写).

  • To specify name of the attribute. If @ModelAttribute is omitted or has the empty value, default name is used (type name of the argument with the first letter decapitalized).

如果参数的类型有特殊含义.例如,如果你的域对象作为属性传递给extends java.security.Principal,你需要对它进行注解,否则Spring会传递一个HttpServletRequest.getUserPrincipal()的结果.

If type of the argument has a special meaning. For example, if your domain object passed as attribute extends java.security.Principal, you need to annotate it, otherwise Spring will pass a result of HttpServletRequest.getUserPrincipal() instead.

有些人倾向于在没有实际需要的情况下使用 @ModelAttribute 来记录参数的含义.

Some people tend to use @ModelAttribute without actual need in order to document the meaning of arguments.

这篇关于@ModelAttribute 注释在方法参数级别的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆