status.setComplete() not clearing the session

穿精又带淫゛_ 提交于 2020-01-25 02:33:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!