简体   繁体   中英

How to add and retrieve an object in session in spring mvc

Is there a way to add an object to the session in Spring controller,(similar to how one would add in a servlet)so that it can be retrieved from the session and updated in another method. I am creating a PDF object in one page, so I have a method in the controller where this object is created. This PDF object is going to be updated in another page, hence there is another method in the controller, where this same PDF should be updated. I was thinking of adding it to the ModelAndView and retrieving it. However, I think it should be in the session. Any idea how to proceed

Adding a Session session parameter to any mapped controller method will make the session available inside a method.

The session will be implicitely created and injected by the framework, so you use it inside a method just like you would inside a Servlet

@RequestMapping(value = "/helloworld", method = RequestMethod.POST)
@ResponseBody
public JsonResponse sayHello(@RequestBody String reqestString ,HttpSession sessionObj)
{
  sessionObj.setAttribute("message" , "It is simple to get access to HttpSession ");
  return jsonResponse;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM