java 在这个 Spring MVC 展示示例中如何使用 @RequestAttribute 和 @ModelAttribute 注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13919307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:26:42  来源:igfitidea点击:

How is used @RequestAttribute and @ModelAttribute annotation in this Spring MVC showcase example?

javaspringspring-mvcmodelannotations

提问by AndreaNobili

I am pretty new in Spring MVC.

我在 Spring MVC 中很新。

In this period I am studying the Spring MVC showcaseexample downlodable from STS dashboard.

在此期间,我正在研究可从 STS 仪表板下载的Spring MVC 展示示例。

I am having some problems understanding how Custom Resolvable Web Arguments are handled in this example.

我在理解此示例中如何处理自定义可解析 Web 参数时遇到了一些问题。

In practice I have the following situation:

在实践中我有以下情况:

In my home.jsp view I have the following link:

在我的 home.jsp 视图中,我有以下链接:

<a id="customArg" class="textLink" href="<c:url value="/data/custom" />">Custom</a> 

This link generate an HTTP Request towards the URL: "/data/custom"

此链接生成一个指向 URL 的 HTTP 请求:“/data/custom”

The controller class that contains the method that handles this request has the following code:

包含处理此请求的方法的控制器类具有以下代码:

@Controller
public class CustomArgumentController {

@ModelAttribute
void beforeInvokingHandlerMethod(HttpServletRequest request) {
    request.setAttribute("foo", "bar");
}


@RequestMapping(value="/data/custom", method=RequestMethod.GET)
public @ResponseBody String custom(@RequestAttribute("foo") String foo) {
    return "Got 'foo' request attribute value '" + foo + "'";
}

 }

The method that handles this HTTP Request is custom()

处理这个 HTTP 请求的方法是 custom()

So when the previous link is clicked the HTTP Request is handled by the custom method...

因此,当单击上一个链接时,HTTP 请求由自定义方法处理...

I am having problems understanding what the @RequestAttributeannotation.

我在理解@RequestAttribute注释的内容时遇到问题。

I think that, in this case, it binds the request attribute named foo to a new String foovariable.

我认为,在这种情况下,它将名为 foo 的请求属性绑定到一个新String foo变量。

But... where is this attribute taken from? Is this variable taken by Spring?

但是……这个属性是从哪里取来的?这个变量是Spring采用的吗?

Ok...my idea is that this request attribute is taken from a HttpServletRequestobject...

好的......我的想法是这个请求属性取自一个HttpServletRequest对象......

I think this because, in this class, I have also have the beforeInvokingHandlerMethod()method that have a speacking name...so it seems that this method seta an attribute, that have name=fooand value=bar, inside an HttpServletRequestobject...and then so the custom()method can use this value...

我认为这是因为,在这个类中,我也有beforeInvokingHandlerMethod()一个具有发音名称的方法......所以似乎这个方法设置了一个属性,在一个对象中具有name=fooand ......然后该方法可以使用这个值...value=barHttpServletRequestcustom()

In fact my output is:

事实上,我的输出是:

Got 'foo' request attribute value 'bar'

Got 'foo' request attribute value 'bar'

Why is the beforeInvokingHandlerMethod()called before the custom()method?

为什么beforeInvokingHandlerMethod()custom()方法之前调用?

And why is the beforeInvokingHandlerMethod()annoted by @ModelAttributeannotation? What does this case mean?

为什么是beforeInvokingHandlerMethod()由annoted@ModelAttribute注释?这个案子是什么意思?

回答by Subin Sebastian

You are correct in assumption of @RequestAttribute, it need not be set in beforeInvokingHandlerMethod. Assume you have a method mapped to /data/initwhich forwards request to /data/custom. In this case request attribute can be set in init method also.

你的假设是正确的@RequestAttribute,它不需要设置在beforeInvokingHandlerMethod。假设您有一个映射到的方法/data/init将请求转发到/data/custom. 在这种情况下,请求属性也可以在 init 方法中设置。

Why the beforeInvokingHandlerMethod() is called before the custom() method?

And why the beforeInvokingHandlerMethod() is annoted by @ModelAttribute annotation? what means in this case?

为什么 beforeInvokingHandlerMethod() 在 custom() 方法之前被调用?

以及为什么 beforeInvokingHandlerMethod() 被@ModelAttribute 注解注解了?在这种情况下是什么意思?

you will get the reason here http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods

你会在这里得到原因 http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods

An @ModelAttribute on a method indicates the purpose of that method is to add one or more model attributes. Such methods support the same argument types as @RequestMapping methods but cannot be mapped directly to requests. Instead @ModelAttribute methods in a controller are invoked before @RequestMapping methods, within the same controller.

方法上的 @ModelAttribute 表示该方法的目的是添加一个或多个模型属性。此类方法支持与@RequestMapping 方法相同的参数类型,但不能直接映射到请求。相反,控制器中的 @ModelAttribute 方法在同一控制器内的 @RequestMapping 方法之前调用。