[英]@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.