了解在Spring MVC中使用@ModelAttribute和@RequestAttribute注释 [英] Understanding the use of @ModelAttribute and @RequestAttribute annotations in Spring MVC
问题描述
我有一些问题要了解在这个例子中如何处理Custom Resolable Web Arguments。
在实践中我有以下情况。在我的 home.jsp 视图中,我有以下链接:
< a id =customArg class =textLinkhref =< c:url value =/ data / custom/>> Custom< / a>
此链接为URL生成HTTP请求:/ data / custom strong>
包含处理此请求的方法的控制器类具有以下代码:
@Controller
public class CustomArgumentController {
@ModelAttribute
void beforeInvokingHandlerMethod(HttpServletRequest request){
request.setAttribute(foo 酒吧);
}
@RequestMapping(value =/ data / custom,method = RequestMethod.GET)
public @ResponseBody字符串自定义(@RequestAttribute(foo)String foo ){
returnGot'foo'request attribute value'+ foo +';
}
}
处理此HTTP请求的方法是 custom()。所以当上一个链接被点击时,HTTP请求是通过自定义方法来处理的。
我有一些问题要明白什么是@RequestAttribute 强>注释做。我认为,在这种情况下,它将名为foo的请求属性绑定到一个新的String foo变量。但是这个属性是从哪里取的?这个变量是由Spring采用的吗?
好的,我的想法是这个请求属性取自HttpServletRequest对象。我认为是这样的,因为在这个类中,我还有一个具有说话名称的 beforeInvokingHandlerMethod()方法,所以看起来这个方法设置一个属性,它们有 name = foo 和 value = bar ,在HttpServletRequest对象中,然后custom()方法可以使用此值。
其实我的输出是:
获取'foo'请求属性值'bar'
为什么 beforeInvokingHandlerMethod()在 custom()方法之前调用?
为什么 beforeInvokingHandlerMethod()由 @ModelAttribute 注释注释?在这种情况下是什么意思?
RequestAttribute
但您在表单提交中传递的参数。让我们了解一个示例
假设我有以下表单
< form action =...>
< input type = hidden name = param1 id = param1 value = test />
< / form>
现在,如果我有下面的控制器映射与请求url映射与表单提交如下。
@Controller
public class CustomArgumentController {
@ModelAttribute
void beforeInvokingHandlerMethod(HttpServletRequest request){
request.setAttribute(foo,bar);
}
@RequestMapping(value =/ data / custom,method = RequestMethod.GET)
public @ResponseBody字符串custom(@RequestAttribute(param1 )String param1){
//这里,我将在String对象中具有param1的值作为测试,它将映射我的Spring本身
}
I am pretty new in Spring MVC. Currently I am studying the Spring MVC Showcase, that demonstrates the features of the Spring MVC web framework.
I have some problem to understand how Custom Resolvable Web Arguments are handled in this example.
In practice I have the following situation. In my home.jsp view I have the following link:
<a id="customArg" class="textLink" href="<c:url value="/data/custom" />">Custom</a>
This link generate an HTTP Request towards the URL: "/data/custom"
The controller class that contain the method that handles this request have 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(). So when the previous link is clicked, the HTTP Request is handled by the custom method.
I have some problem to understand what exactly does the @RequestAttribute annotation do. I think that, in this case, it binds the request attribute named foo to a new String foo variable. But where this attribute is taken from? Is this variable taken by Spring?
OK, my idea is that this request attribute is taken from a HttpServletRequest object. I think so because, in this class, I also have the beforeInvokingHandlerMethod() method that have a speaking name, so it seems that this method sets an attribute, that have name=foo and value=bar, inside an HttpServletRequest object, and then so the custom() method can use this value.
In fact my output is:
Got 'foo' request attribute value 'bar'
Why the beforeInvokingHandlerMethod() is called before the custom() method?
And why the beforeInvokingHandlerMethod() is annotated by @ModelAttribute annotation? What does it mean in this case?
The RequestAttribute
is nothing but the parameters which you have passed in the form submission. Lets understand with a sample example
Suppose I have the below form
<form action="...">
<input type=hidden name=param1 id=param1 value=test/>
</form>
Now, if I have the below controller which is mapped with the request url which is mapped with the form submission as below.
@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("param1") String param1 ) {
// Here, I will have value of param1 as test in String object which will be mapped my Spring itself
}
这篇关于了解在Spring MVC中使用@ModelAttribute和@RequestAttribute注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!