Spring MVC - Variables Between Pages and Unsetting SessionAttribute

The question sounds strange, I play with Spring MVC and try to move between two pages, and basically I create a JSP page using Spring Form JSTL, so it just uses POST, and I use the controller to go from one page to another. But models are lost from page to page, and I would like to hide the actual variable, so QueryStrings is out of the question (as far as I know). I know that I can use InternalResourceView, but only allows me to use the model.

I want to pass a variable that will be exclusive to this page, what is the best way without a model or using QueryStrings?

I planned to use SessionAttribute to easily define them, but wondered how to remove the variable created by SessionAttribute? I tried HttpSession.removeAttribute and didn't seem to work.

+4
source share
4 answers

You can also use SessionStatus.setComplete () as follows:

@RequestMapping(method = RequestMethod.GET, value="/clear") public ModelAndView clear(SessionStatus status, ModelMap model, HttpServletRequest request) { model.clear(); status.setComplete(); return new ModelAndView("somePage"); } 

or DefaultSessionAttributeStore.cleanUpAttribute:

 @RequestMapping(method = RequestMethod.GET, value="/clear") public ModelAndView clear(DefaultSessionAttributeStore status, WebRequest request, ModelMap model) { model.remove("mySessionVar"); status.cleanupAttribute(request, "mySessionVar"); return new ModelAndView("somePage"); } 

I use it the way it is on one of my forms, which has a mulitple sessionAttributes, and I want to delete only one of them.

+5
source

You can use the removeAttribute method from the HttpSession class.

+3
source

you can use WebRequest.removeAttribute(String name, int scope) that works with Spring @SessionAttributes . Quote from @SessionAttributes javadoc - "As an alternative, consider using the common interface attribute management capabilities {@link org.springframework.web.context.request.WebRequest}.

Also look at my example.

 @Controller @SessionAttributes({"sessionAttr"}) public class MyController { @ModelAttribute("sessionAttr") public Object defaultSessionAttr() { return new Object(); } @RequestMapping(value = "...", method = RequestMethod.GET) public String removeSessionAttr(WebRequest request, Model model) { request.removeAttribute("sessionAttr", WebRequest.SCOPE_SESSION); model.addAttribute("sessionAttr", defaultSessionAttr()); return "myView"; } } 
+2
source

All Articles