简体   繁体   中英

status.setComplete() not clearing the session

I am trying to clear a session. Below is a little sample code I wrote to check with SessionStatus .

@Controller
@SessionAttributes(value={"sessAttr1","sessAttr2","sessAttr3"})
public class SessionController {    
@RequestMapping(value="index")
public ModelAndView populateSession(){      
    ModelAndView modelView = new ModelAndView("home");
    modelView.addObject("sessAttr1","This value is added in a session 1");
    modelView.addObject("sessAttr2","This value is added in a session 2");
    modelView.addObject("sessAttr3","This value is added in a session 3");
    return modelView;
}

@RequestMapping(value="home1")
public String populateHomeSession(SessionStatus status){
    status.setComplete();
    return "home1";
}
}

When home1 screen is displayed, I can still see the session objects not getting cleared. If I try this: ${sessAttr1 } then I can read the session values in home1 screen.

Please clarify why is it not working.

EDIT

I an using <a href="home1">Next</a> to navigate from one screen to another. Does it have something to do with this isse I am facing?

setComplete is used to mark a session attribute as not needed after the request has been processed by the controller: It does not immediately modify the session, and it overall sounds like it's a poor fit for your use case. It's intended to be used in a situation like a POST where the data is intended to be used during the present request, but should not be used in the future.

http://forum.spring.io/forum/spring-projects/web/108339-problem-with-sessionattribute-and-sessionstatus

you can use invalidate()

@RequestMapping(value="home1")
public String populateHomeSession(HttpservletRequest request){

HttpSession session=request.getSession();
session.invalidate();

    return "home1";
}

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