Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - Variables between pages, and Unsetting a SessionAttribute

The question sounds weird, I'm playing around with Spring MVC and am trying to move between two pages and basically I'm creating a JSP page using Spring Form JSTL's so it just uses a POST, and I use a controller to move from one page to the next. But Models are lost from page to page, and I'd like to hide the actual variable so QueryStrings are out of the question(as far as I know). I know I can use a InternalResourceView, but only allows me to use a model.

I want to transfer a variable that will be exclusive to that page, what's the best way without a model or using QueryStrings?

I was planning on using SessionAttribute to easily define them, but was wondering, how do you remove a SessionAttribute created variable? I tried HttpSession.removeAttribute and it didn't seem to work.

like image 878
Nicholas Avatar asked Dec 10 '22 08:12

Nicholas


2 Answers

You can also use SessionStatus.setComplete() like this:

@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 like this:

@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 like this on one of my forms that has mulitple sessionAttributes and I want to remove only one of them.

like image 105
blong824 Avatar answered Dec 13 '22 21:12

blong824


Yes... HttpSession.removeAttribute

like image 35
Andrew White Avatar answered Dec 13 '22 22:12

Andrew White