19

Example of using @SessionAttributes below. How to clear user session attribute after wizard finished ? In my example after returning to /wizard0 session attribute still exists. I've tried status.setComplete() and session.removeAttribute("user") but it doesn't work.

@Controller
@SessionAttributes("user")
public class UserWizard {

    @RequestMapping(value = "/wizard0", method = RequestMethod.GET)
    public String page1(Model model) {
        if(!model.containsAttribute("user")) {
            model.addAttribute("user", new User());
        }
        return "wizard/page1";
    }

    @RequestMapping(value = "/wizard1", method = RequestMethod.GET)
    public String page2(@ModelAttribute User user) {
        user.setFirstname(Utils.randomString());
        return "wizard/page2";
    }

    @RequestMapping(value = "/wizard2", method = RequestMethod.GET)
    public String page3(@ModelAttribute User user) {
        user.setLastname(Utils.randomString());
        return "wizard/page3";
    }

    @RequestMapping(value = "/finish", method = RequestMethod.GET)
    public String page4(@ModelAttribute User user, HttpSession session, SessionStatus status) {
        /**
         * store User ...
         */
        status.setComplete();
        session.removeAttribute("user");
        return "redirect:/home";
    }

}

EDIT

My mistake. status.setComplete(); works good. session.removeAttribute("user") is nothing to do here.

marioosh
  • 27,328
  • 49
  • 143
  • 192
  • `status.setComplete();` supposed to clean up your `SessionAttribute`. Maybe you are setting the user in another class or it's some problem with your session configuration... – Deividi Cavarzan Aug 13 '13 at 13:44
  • have u confirmed that the session contains the same user object that you added? check the hashcode of user object after and before removing it frm session – coder Aug 13 '13 at 14:36
  • 1
    **Sorry. My mistake**. `status.setComplete()` works good however. I have finish button not mapped correctly - method `page4` wasn't called at all, so that was a problem. I saw that `session.removeAttribute("user") / HttpSession` has nothing to do with `@SessionAttributes`. Question to close/delete/etc... Sorry once again. – marioosh Aug 14 '13 at 06:18

2 Answers2

14

Try to use WebRequest.removeAttribute method instead of HttpSession.setAttribute method (example 1). Or another way which do exactly the same you can use 'SessionAttributeStore.cleanupAttribute' (example 2).

EXAMPLE 1

@RequestMapping(value = "/finish", method = RequestMethod.GET)
public String page4(@ModelAttribute User user, WebRequest request, SessionStatus status) {
    /**
     * store User ...
     */
    status.setComplete();
    request.removeAttribute("user", WebRequest.SCOPE_SESSION);
    return "redirect:/home";
}

EXAMPLE 2

@RequestMapping(value = "/finish", method = RequestMethod.GET)
public String page4(@ModelAttribute User user, WebRequest request, SessionAttributeStore store, SessionStatus status) {
    /**
     * store User ...
     */
    status.setComplete();
    store.cleanupAttribute(request, "user");
    return "redirect:/home";
}
michal.kreuzman
  • 12,170
  • 10
  • 58
  • 70
  • 1
    How to instantiate **SessionAttributeStore** because I have expectation: `Could not instantiate bean class [org.springframework.web.bind.support.SessionAttributeStore]: Specified class is an interface`. – Anton Dozortsev Apr 23 '14 at 07:48
  • How to clean up all session attributes in one go? – Amit Kumar Oct 11 '15 at 18:20
0

Below worked for me -

@RequestMapping(value = "/finish", method = RequestMethod.GET)
public String page4(HttpSession httpsession, SessionStatus status) {

/*Mark the current handler's session processing as complete, allowing for cleanup of 
  session attributes.*/
status.setComplete();

/* Invalidates this session then unbinds any objects boundto it. */
httpsession.invalidate();
return "redirect:/home";
}
abhijeet badale
  • 149
  • 1
  • 3