【问题标题】:How is used @RequestAttribute and @ModelAttribute annotation in this Spring MVC showcase example?在这个 Spring MVC 展示示例中如何使用 @RequestAttribute 和 @ModelAttribute 注释?
【发布时间】:2012-12-04 20:27:54
【问题描述】:

我是 Spring MVC 的新手。

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

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

在实践中我有以下情况:

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

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

此链接会生成一个指向以下 URL 的 HTTP 请求:"/data/custom"

包含处理这个请求的方法的控制器类有如下代码:

@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()

所以当点击上一个链接时,HTTP 请求由自定义方法处理...

我无法理解 @RequestAttribute 注释的内容。

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

但是……这个属性是从哪里得到的?这个变量是 Spring 占用的吗?

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

我认为这是因为,在这个类中,我也有 beforeInvokingHandlerMethod() 方法,它有一个speaking name...所以看起来这个方法设置了一个属性,里面有 name=foo 和 value=bar一个HttpServletRequest 对象...然后custom() 方法可以使用这个值...

其实我的输出是:

Got 'foo' request attribute value 'bar'

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

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

【问题讨论】:

    标签: java spring spring-mvc model annotations


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • mmm 我很难理解我发布的示例 tath 中到底发生了什么......所以阅读你发布的内容我明白如果我有一个命令类,其中包含 RequestMapping 注释的某些方法注释和由 ModelAttribute 注释的其他一些方法...这些第二种类型的方法在 RequestMapping 注释的方法之前被调用...所以这个方法做一些类似于初始化的事情在我的情况下到底发生了什么?我在传递的 HttpServletRequest 中存储了一个名为 foo 的属性...ok 现在执行了只提取属性值的 custom() 方法?
    • 是的 @ModelAttribute 方法将首先被调用,在控制器类中的任何请求处理程序之前。您在该方法中所做的是设置请求属性,您可以在处理程序方法中检索该属性。
    • String custom(@RequestAttribute("foo") foo 这是一个方便的方法。否则你将不得不写String foo=(String) request.getAttribute("foo");
    • mmm...好吧...关于这件事的最后一个澄清:所以我将一个名为“foo”的属性放入一个 HttpServletRequest 对象(即创建的对象)中,该属性具有 value="bar"由 web 容器,其中包含有关请求参数和会话信息的信息)澄清是关于 RequestAttribute 注释......做: RequestAttribute("foo") String foo 表示我正在通过 HttpServletRequest 获取名为 foo 的属性?是真的吗?或从哪里来?谢谢安德里亚
    • spring 正在调用你的方法custom()。因为它带有 @requestMapping 注释,所以在调用它之前,spring 会检查它的参数,输入并填充它们。
    猜你喜欢
    • 2012-12-03
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多