什么是@ModelAttribute注解的方法参数层面的含义? [英] What is the meaning of @ModelAttribute annotation at method argument level?

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

问题描述

春季3参考教导我们:

当你将它放在一个方法
  参数 @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.

要解决这个矛盾,我去了计算器,发现这个详细的例子。
例如笔者说:

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...

要打消我部署了示例应用程序在我的环境魅力和无情地夺去@ModelAttribute注解从方法 MainController.saveEdit()。作为结果的应用程序的工作没有任何变化!所以,我的结论是:不需要 @ModelAttribute 注释Web表单的字段值传递给参数的领域。然后我坚持这样的问题:什么是 @ModelAttribute 标注的是什么意思?如果仅仅意味着是设置别名,在视图中,那么为什么这种方式比增加​​明确的对象为 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 是可选的 - 如果参数没有被标注和它的类型没有特别的这意味着(即它不是的HttpServletRequest ModelMap 等),它就像对待 @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 被省略或具有空的,则使用缺省名称(第一个参数的类型名称字母大写化)。

  • 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).

如果参数类型有特殊的意义。例如,如果作为属性传递您的域对象扩展的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天全站免登陆