简体   繁体   English

Spring MVC项目中的@RequestAttribute不会获取值

[英]@RequestAttribute in a Spring MVC project does not fetch the value

@RequestAttribute in a Spring MVC project doesn't fetch the value. Spring MVC项目中的@RequestAttribute不会获取值。

I use a @ModelAttribute . 我使用@ModelAttribute Here foo attribute is set a value of bar 这里的foo属性设置为bar的值

@ModelAttribute  
void beforeInvokingHandlerMethod(HttpServletRequest request) 
{  
    request.setAttribute("foo", "bar");  
}

I try to invoke the request attribute value for foo using @RequestAttribute("foo") . 我尝试使用@RequestAttribute("foo")foo调用请求属性值。 But value is null. 但是值是空的。

Then I try using request.getAttribute("foo") and the value is printed. 然后,我尝试使用request.getAttribute("foo")并打印该值。 I don't know what is wrong in the following code: 我不知道以下代码有什么问题:

@RequestAttribute("foo"). 
@RequestMapping(value="/data/custom", method=RequestMethod.GET)  
public @ResponseBody String custom(@RequestAttribute("foo") String foo, HttpServletRequest request) {  
    System.out.println("foo value : " + foo);    //null printed  
    System.out.println("request.getAttribute : " + request.getAttribute("foo"));    //value printed  

    return foo;  
}

@RequestAttribute is not a Spring annotation. @RequestAttribute不是Spring注释。 If you want to pass a value a request param you can do 如果您想传递值请求参数,则可以执行

@RequestMapping(value="/data/custom", method=RequestMethod.GET)  
public @ResponseBody String custom(@RequestParam("foo") String foo) {  
    System.out.println("foo value : " + foo);    //null printed      
    return foo;  
}

Or if you want to pass values in the path you can do 或者,如果您想在路径中传递值,则可以执行

@RequestMapping(value="/data/custom/{foo}", method=RequestMethod.GET)  
public @ResponseBody String custom(@PathVariable("foo") String foo) {  
    System.out.println("foo value : " + foo);    //null printed      
    return foo;  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 了解在Spring MVC中使用@ModelAttribute和@RequestAttribute注释 - Understanding the use of @ModelAttribute and @RequestAttribute annotations in Spring MVC 在此Spring MVC展示示例中,如何使用@RequestAttribute和@ModelAttribute批注? - How is used @RequestAttribute and @ModelAttribute annotation in this Spring MVC showcase example? 如何使用 postman 或 feign 客户端在 Spring 引导中将值设置为 @RequestAttribute - How to set value to @RequestAttribute in Spring boot using postman or feign client Spring MVC无法获取JSON值 - Spring MVC can not fetch JSON value Spring MVC教程项目不是基于IntelliJ构建的 - Spring MVC tutorial project does not build on IntelliJ Java Spring MVC Maven项目不起作用 - Java Spring MVC Maven project does not work Spring 安全注释@EnableWebSecurity 在 Spring MVC 项目中不起作用 - Spring Security Anotation @EnableWebSecurity does not works in Spring MVC project spring mvc项目不返回jsp页面(HTTP状态404) - spring mvc project does not return jsp page (HTTP Status 404) Spring Boot中一个简单的REST项目是否使用了MVC原理? - Does a simple REST project in Spring Boot use the MVC principle? spring mvc project不返回jsp页面的HTTP状态404 - spring mvc project does not return jsp page HTTP Status 404
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM