有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

是否有任何java/spring方法可以使用HttpServletRequest存储当前登录用户的会话信息?

我想知道是否可以在HttpServletRequest对象上设置属性

我想做的是为当前登录的用户存储一些信息,以便稍后(在同一会话中)返回

我正在使用SpringMVC

到目前为止我试过这个

@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);
}

到目前为止,当我做req.getAttribute时,我得到一个空值。。。有什么想法吗


共 (1) 个答案

  1. # 1 楼答案

    To setAttributein session应该这样使用:

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

    你可以这样得到:

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