Spring MVC3访问HttpServletRequest

发布于 2024-12-21 04:34:10 字数 580 浏览 2 评论 0原文

我想自己处理请求和会话属性,而不是将其留给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥醉 2024-12-28 04:34:10

连接调试器并确认您的控制器方法被调用。

检查以下内容:

  1. 您是否正在发送 POST 请求?然后需要用方法参数指定方法名:@RequestMapping(value = "/hello", method = RequestMethod.POST)

  2. Spring 通过 正确检测到您的控制器吗? >@Controller注释?

Attach a debugger and confirm that your controller method is called.

Check the following:

  1. Are you sending a POST request? Then you need to specify the method name with the method parameter: @RequestMapping(value = "/hello", method = RequestMethod.POST)

  2. Is your controller properly detected by Spring via the @Controller annotation?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文