问题
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?
回答1:
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
回答2:
you can use invalidate()
@RequestMapping(value="home1")
public String populateHomeSession(HttpservletRequest request){
HttpSession session=request.getSession();
session.invalidate();
return "home1";
}
来源:https://stackoverflow.com/questions/24342687/status-setcomplete-not-clearing-the-session