简体   繁体   中英

spring clear session scope bean

I have created a bean of session scope as per below:

@Bean
@Scope(
    value = WebApplicationContext.SCOPE_SESSION,
    proxyMode = ScopedProxyMode.TARGET_CLASS)
public TodoList todos() {
    return new TodoList();
}

I have added the bean as a model attribute and I could display the beans across multiple pages.

@GetMapping("/todos.html")
public String list(Model model) {
    model.addAttribute("todos", todos);
    return "scopedproxytodos";
}

But I am not sure of how to clear the attribute in the session.

@PostMapping("/end")
public ModelAndView endSession(SessionStatus sessionStatus, Model model) {
    model.addAttribute("todos", new TodoList());
    sessionStatus.setComplete();
    return new ModelAndView("redirect:/");
}

I have added the bean as a model attribute and I could display the beans across multiple pages :

...

 `model.addAttribute("todos", todos);` 

...

Not really. That adds the attribute only in the current view.

To achieve that you should annotate your TodoList class with @SessionScope such as :

@SessionScope
@Component
public class TodoList {
    // ...
}

Then annotate your controllers that need to have a conversational scope for todos with @SessionAttributes and inject that with @ModelAttribute . Not tested but something like that :

@Controller
@SessionAttributes("todos")
public class FooController { 

  @GetMapping("/todos.html")
  public String list(@ModelAttribute TodoList todos) {
    // set the todos value
    return "scopedproxytodos";
  }

}

You can use that in any other controller annotated with @SessionAttributes("todos") .

And to remove todos of the current conversation session, use status.setComplete() as you did.
In your case you can just remove that :

@PostMapping("/end")
public ModelAndView endSession(SessionStatus sessionStatus, Model model) {
    sessionStatus.setComplete();
    return new ModelAndView("redirect:/");
}

And let the controller with the conversational scope to reinit it as you want ( FooController does that actually).
An alternative way is reinitializing that instead of removing that.
For example :

@PostMapping("/end")
public ModelAndView endSession(@ModelAttribute TodoList todos) {
    todos.clear();
    return new ModelAndView("redirect:/");
}

The advantage of this solution :

  • neater / less verbose
  • not needed to create beans for each session. Beans are more expensive in terms of overhead than values stored in the session.

More information here about that way of manipulate conversational attributes.


The SessionStatus class manages the attributes of the spring session :

Simple interface that can be injected into handler methods, allowing them to signal that their session processing is complete. The handler invoker may then follow up with appropriate cleanup, eg of session attributes which have been implicitly created during this handler's processing

It means that it clears the attributes stored in the Spring handler session (that is different from a classic http session), not the attributes stored in the http requests and actually you didn't add anything in the spring session attributes.
So that is useless.

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