springmvc 框架中,controller获取session的方法,有5种,这里介绍常用的3种:
方法一
将HttpSession作为 controller 的方法参数传入,直接获取。
@RequestMapping("/test")
public void test(HttpSession session){
}
方法二
将HttpServletRequest作为 controller 的方法参数传入,然后再获取session
@RequestMapping("/test2")
public void test2(HttpServletRequest request){
HttpSession session = request.getSession();
}
方法三
通过类RequestContextHolder获取
HttpServletRequest req=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session=req.getSession();