- Mastering Spring 5.0
- Ranga Rao Karanam
- 106字
- 2021-07-02 22:12:18
Removing an attribute from the session
It is important to remove values from the session when they are no longer needed. There are two ways in which we can remove values from the session conversational state. The first way is demonstrated in the following snippet. It uses the removeAttribute method available in the WebRequest class:
@RequestMapping(value="/some-method",method = RequestMethod.GET)
public String someMethod(/*Other Parameters*/
WebRequest request, SessionStatus status) {
status.setComplete();
request.removeAttribute("exampleSessionAttribute",
WebRequest.SCOPE_SESSION);
//Other Logic
}
This example shows the second approach using the cleanUpAttribute method in SessionAttributeStore:
@RequestMapping(value = "/some-other-method",
method = RequestMethod.GET)
public String someOtherMethod(/*Other Parameters*/
SessionAttributeStore store, SessionStatus status) {
status.setComplete();
store.cleanupAttribute(request, "exampleSessionAttribute");
//Other Logic
}