Spring MVC - 会话差异 [英] Spring MVC - Session differences
本文介绍了Spring MVC - 会话差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过 HttpServletRequest.getSession()
和 HttpSession
在控制器方法中注入会话之间是否有任何区别?
Are there any differences between getting session through HttpServletRequest.getSession()
and HttpSession
injected in controller's method?
推荐答案
基本上,会话
对象注入 Spring MVC 控制器:
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
}
从 HttpServletRequest中检索的
:会话
对象
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.
}
前一种风格只为您提供了获取上下文<$ c的工具$ c> HttpSession 将对象注入控制器参数,以便 Spring 为你处理所有脏东西。
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.
这篇关于Spring MVC - 会话差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文