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

查看:94
本文介绍了在此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请求:"/数据/自定义"

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

实际上我的输出是:

Got 'foo' request attribute value 'bar'

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

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.

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

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

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

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

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

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

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天全站免登陆