问题描述
我在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=foo
和value=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.
为什么@ModelAttribute批注会注释beforeInvokingHandlerMethod()?在这种情况下是什么意思?
And why the beforeInvokingHandlerMethod() is annoted by @ModelAttribute annotation? what means in this case?
you will get the reason herehttp://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods
这篇关于在此Spring MVC展示示例中,如何使用@RequestAttribute和@ModelAttribute批注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!