How to get session information in Spring MVC 3

后端 未结 2 1761
长发绾君心
长发绾君心 2021-02-04 01:52

I am new to spring MVC and I am trying to get the session information in my controller class

Right now I am using

HttpSession objHttpSession = re

2条回答
  •  余生分开走
    2021-02-04 01:57

    You can get the session in Spring MVC like this:

    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    HttpSession session = attr.getRequest().getSession();
    

    The currentRequestAttributes method return the RequestAttributes currently bound to the thread in which there is a current request and from that request you can get the session. This is useful when you need to get hold of the session in non-spring class. Otherwise you can just use:

    @RequestMapping(...)
    public void myMethod(HttpSession session) {   
    }
    

    Spring will inject HttpSession instance into your controller's method.

提交回复
热议问题