在方法参数上使用@ModelAttribute [英] Using @ModelAttribute on a method argument

查看:373
本文介绍了在方法参数上使用@ModelAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如此链接
在方法参数上使用@ModelAttribute


方法参数的@ModelAttribute表示从模型中检索
的参数。

An @ModelAttribute on a method argument indicates the argument should be retrieved from the model.

但我观察到即使没有用@ModelAttribute注释UserDetail, userDetail已正确填充。以下是相关的代码段

But i have observed that even without annotating UserDetail with @ModelAttribute , userDetail is populated correctly. Here is relevant code snippet

<form:form id="userForm" action="path/userDetail" method="post" commandName="userDetail">

    @RequestMapping(value="/userDetail", method=RequestMethod.POST)
    public String processUserDetail(UserDetail userDetail, HttpServletRequest request, HttpServletResponse response, Locale locale) 
    {}

所以我的问题是spring本身填充处理程序方法中存在的项目自定义对象(在本例中为userDetail)参数
甚至没有注释@ModelAttribute。我相信@ModelAttribute在渲染视图时扮演角色但是在提交弹簧
时会自动填充方法参数(如果它存在于模型中)?

So my question does spring itself populate the project custom objects (in this case userDetail) present in handler method arguments even without annotating @ModelAttribute. I believe @ModelAttribute plays role while rendering view but while submission spring automatically populates the method argument if it is present in model ?

推荐答案

TLDR



当您只关心填充自己类型的实例时,无关紧要。

TLDR

When all you care is to populate an instance of your own type it doesn't matter.

在大多数情况下,如果添加 @ModelAttribute ,或者它没有区别省略它。正如@MasterSlave已经提到的,在幕后使用相同的机制。在我谈到使用 @ModelAttribute 的重要性之前,让我解释一下该模型究竟是什么。

In most cases it doesn't make a difference if you add @ModelAttribute or omit it. As @MasterSlave already mentioned the same mechanism is used behind the scenes. Before I talk about when it's important to use @ModelAttribute let me explain what the model actually is.

来自正在呈现的视图的点,它是视图可以访问的数据。在您的情况下 userDetail 。控制器负责将所有必要的数据添加到模型中。有很多方法可以实现这一目标。 @ModelAttribute 就是其中之一。

From the point of a view, that is being rendered, it's the data that can be accessed by the view. In your case the userDetail. The controller is responsible to add all necessary data to the model. There are many ways to achieve that. @ModelAttribute is one of them.

显式使用 @ModelAttribute 的一个用例是定义可以访问数据的密钥。 @ModelAttribute(user)UserDetail user 让视图通过键 user 访问数据。否则它将是 userDetail

One use case for explicitly using @ModelAttribute is to define the key by which the data can be accessed. @ModelAttribute("user") UserDetail user let's the view access the data by the key user. Otherwise it would be userDetail.

数据可能已经存在之前控制器方法被调用。也许它存储在会话中或由另一个方法生成。那是什么意思

The data may also already exist before the controller method is called. Maybe it's stored in the session or generated by another method. That's what is meant by


应该从模型中检索参数

the argument should be retrieved from the model

但它可能存在于特定键下,例如user。所以,你必须通过 @ModelAttribute(user)提供名称。现有数据也可以是简单类型,如 String Date 。在这种情况下, @ModelAttribute 是从模型中检索数据所必需的。

But it may exist under a specific key, like "user". So again, you have to provide the name via @ModelAttribute("user"). The existing data could also be a "simple" type, like a String or a Date. In that case @ModelAttribute is necessary to retrieve the data from the model.

对于其他解析器将处理的类型,例如 Map Locale

The same is true for arguments of types that would be handled by other resolvers, like Map or Locale.

这篇关于在方法参数上使用@ModelAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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