简体   繁体   English

是否有任何Java / spring方式使用HttpServletRequest为当前登录用户存储会话信息?

[英]is there any java/spring way to store session information for the current logged user using the HttpServletRequest?

I am wondering if I can set attributes on the HttpServletRequest object. 我想知道是否可以在HttpServletRequest对象上设置属性。

What I want to do is to store some information for the current logged user that I can later get back (on the same session). 我要做的是为当前登录的用户存储一些信息,以后我可以在同一会话中找回这些信息。

I am using spring mvc. 我正在使用Spring MVC。

So far I tried this 到目前为止,我尝试了

@RequestMapping(value = "/url1", method = RequestMethod.GET)
public void test1(final HttpServletRequest req, final ModelMap model) {
    List<String> myList = (List<String>)req.getAttribute("myList");
}

@RequestMapping(value = "/url2", method = RequestMethod.GET)
public void test2(final HttpServletRequest req, final ModelMap model) {
    String message = "hello world";
    List<String> messages = new ArrayList<String>();
    messages.add(messages);
    req.setAttribute("myList", messages);
}

So far, when I make the req.getAttribute I get a null... Any idea? 到目前为止,当我制作req.getAttribute我得到一个空值。

To setAttribute in session should be used like this: 要在session中使用setAttribute应该像这样:

 request.getSession().setAttribute("myList", messages);

And you can get it like this : 你可以这样得到:

 request.getSession().getAttribute("myList");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM