简体   繁体   中英

Spring MVC - Session differences

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

Basically there is no diffrerence between the session object injected into a Spring MVC controller:

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

And the session object retrieved from the 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.
}

The former style just provide you with a facility to get the contextual HttpSession object by injecting it as a controller argument so that Spring takes care of the all the dirty stuff for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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