在这个 Spring MVC 展示示例中如何使用 @RequestAttribute 和 @ModelAttribute 注释? [英] How is used @RequestAttribute and @ModelAttribute annotation in this Spring MVC showcase example?

查看:26
本文介绍了在这个 Spring MVC 展示示例中如何使用 @RequestAttribute 和 @ModelAttribute 注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring MVC 的新手.

I am pretty new in Spring MVC.

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

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

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

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

在实践中我有以下情况:

In practice I have the following situation:

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

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

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

此链接生成指向 URL 的 HTTP 请求:"/data/custom"

This link generate an HTTP Request towards the URL: "/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 + "'";
}

 }

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

The method that handles this HTTP Request is custom()

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

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

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

I am having problems understanding what the @RequestAttribute annotation.

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

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

但是...这个属性是从哪里获取的?这个变量是Spring取的吗?

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

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

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

我认为这是因为,在这个类中,我还有 beforeInvokingHandlerMethod() 方法,它有一个响亮的名字......所以似乎这个方法设置了一个属性,它有 name=foovalue=bar,在 HttpServletRequest 对象中...然后 custom() 方法可以使用这个值...

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=foo and value=bar, inside an HttpServletRequest object...and then so the custom() method can use this value...

实际上我的输出是:

得到 'foo' 请求属性值 'bar'

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

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

为什么beforeInvokingHandlerMethod()@ModelAttribute注解了?这个案子是什么意思?

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

推荐答案

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

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

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

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

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

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

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

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

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.

这篇关于在这个 Spring MVC 展示示例中如何使用 @RequestAttribute 和 @ModelAttribute 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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