java – Spring MVC – 会话差异

通过HttpServletRequest.getSession()获取会话和在控制器方法中注入HttpSession之间有什么区别吗?

解决方法:

注入Spring MVC控制器的会话对象基本上没有区别:

@RequestMapping(value = "/somepath", method = RequestMethod.POST)
@ResponseBody
public JsonResponse someMethod (HttpSession session)
{
 // play with session attributes
}

从HttpServletRequest中检索的会话对象:

@RequestMapping(value = "/somepath", method = RequestMethod.POST)
@ResponseBody
public JsonResponse someMethod (HttpServletRequest request)
{
  Session session = request.getSession();
  // You are playin with the same session attributes.
}

前一种风格只是通过将它作为控制器参数注入来为您提供获取上下文HttpSession对象的工具,以便Spring为您处理所有脏东西.

上一篇:java – Sturts 2会话失效,将请求会话设置为新会话


下一篇:java-所有servlet容器都使用jsessionid来跟踪会话吗?