通过HttpServletRequest.getSession()
获取session和在controller方法中注入(inject)HttpSession
有区别吗?
最佳答案
注入(inject) Spring MVC Controller 的 session
对象基本上没有区别:
@RequestMapping(value = "/somepath", method = RequestMethod.POST)
@ResponseBody
public JsonResponse someMethod (HttpSession session)
{
// play with session attributes
}
以及从 HttpServletRequest
中检索到的 session
对象:
@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
对象的工具,方法是将它作为 Controller 参数注入(inject),以便 Spring 处理所有脏东西给你。
关于java - Spring MVC - session 差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873446/