【问题标题】:Manual Session in Spring MVC Not WorkingSpring MVC 中的手动会话不起作用
【发布时间】:2012-09-26 04:57:52
【问题描述】:

我正在尝试设置会话变量并在将来的请求中检索它。我看到在 Spring MVC 中有几种方法可以做到这一点。我尝试了几种方法,但没有一种方法有效。我回到最简单的方法(手动会话操作),仍然没有运气。

这是我的代码:

@Controller
public class Test {

private static final Logger LOG = Logger.getLogger(Test.class);

@RequestMapping({ "/set", "/set/" })
public final ModelAndView set(
        final HttpServletRequest request,
        final HttpServletResponse response, 
        final HttpSession session) {

    LOG.info("setting the session");
    session.setAttribute("userId", new Object());
    request.getSession(true).setAttribute("userId", new Object());

    return new ModelAndView("someView");
}

@RequestMapping({ "/get", "/get/" })
public final ModelAndView get(
        final HttpServletRequest request,
        final HttpServletResponse response, 
        final HttpSession session) {

    final HttpSession session1 = request.getSession();
    final Object userId = session.getAttribute("userId");
    final Object userId1 = session1.getAttribute("userId");
    LOG.info("and the userId is '" + userId + "' '" + userId1 + "'");

    return new ModelAndView("someView");
}
}

我在 /set 访问我的服务器,然后在 /get。我查看我的日志并查看:

setting the session
and the userId is 'null' 'null'

为什么这不起作用?

【问题讨论】:

    标签: session spring-mvc tomcat6


    【解决方案1】:

    我不知道为什么,但是将以下内容放入我的 web.xml 中解决了问题:

      <listener>
        <listener-class>
          org.springframework.web.context.request.RequestContextListener
        </listener-class>
      </listener>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多