Spring MVC3访问HttpServletRequest
我想自己处理请求和会话属性,而不是将其留给 Spring @SessionAttributes
,例如用于登录或 cookie 处理。 我只是不知道如何从控制器内访问 HttpRequest 。我需要一种方法来访问 @RequestAttribute
之上的一层并访问 HttpRequest
本身。 Stripes 用于通过实现 ApplicationContext
并调用 getAttribute()
来实现此目的。
另外,将 HttpServletRequest 作为参数传递似乎不起作用:
@RequestMapping(value="/")
public String home(HttpServletRequest request){
System.out.println(""+request.getSession().getCreationTime());
return "home";
}
为什么此方法不打印任何内容?
I would like to handle request and session attributes myself rather than leaving it to Spring @SessionAttributes
, for login or cookies handling for example.
I just cannot figure out how to access the HttpRequest
from within a controller. I need a way to go a layer above the @RequestAttribute
and access the HttpRequest
itself.
With Stripes in used to do this by implementing an ApplicationContext
and calling getAttribute()
.
Also, passing the HttpServletRequest
as parameter seems not to work:
@RequestMapping(value="/")
public String home(HttpServletRequest request){
System.out.println(""+request.getSession().getCreationTime());
return "home";
}
Why does this method not print anything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
连接调试器并确认您的控制器方法被调用。
检查以下内容:
您是否正在发送 POST 请求?然后需要用方法参数指定方法名:
@RequestMapping(value = "/hello", method = RequestMethod.POST)
Spring 通过
正确检测到您的控制器吗? >@Controller
注释?Attach a debugger and confirm that your controller method is called.
Check the following:
Are you sending a POST request? Then you need to specify the method name with the method parameter:
@RequestMapping(value = "/hello", method = RequestMethod.POST)
Is your controller properly detected by Spring via the
@Controller
annotation?