I am trying to understand how to sending HttpSession as a parameter in the spring controller works. I have a jsp which does a post request on clicking the submit button. In the controller, reading the sessions as follows
In the controller:
public ModelAndView viewEditFundClass(HttpServletRequest request,HttpServletResponse response,Model model){
HttpSession session = (HttpSession)request.getSession();
java.util.Date startDate = sesseion.getAttribute("startDate");
However, when I just change the controller to the following, I am still able to access the session
public ModelAndView viewEditFundClass(HttpServletRequest request,HttpServletResponse response, HttpSession session,Model model)
I would like to know how this is done in Spring, ie how did the post request pass the HttpSession as a parameter? will this session be valid?
Assuming you're using Spring 3+ @Controller
and @RequestMapping
handler methods, Spring defines a default set of supported argument types for your handlers
- Session object (Servlet API): of type
HttpSession
. An argument of this type enforces the presence of a corresponding session. As a consequence, such an argument is nevernull
.
Spring uses the strategy pattern to accomplish this, using the interface HandlerMethodArgumentResolver
. It checks the parameter types of your handler methods and, for each type, tries to find a HandlerMethodArgumentResolver
that will be able to resolve an argument for it.
For HttpSession
, that implementation is ServletRequestMethodArgumentResolver
.