Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: How to remove session attribute?

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.

like image 778
marioosh Avatar asked Aug 13 '13 12:08

marioosh


People also ask

How do I delete a session attribute?

In order to remove/delete session completely, use session. invalidate() method. If you just want to remove a particular attribute then use session. removeAttribute(attribute_name) but make sure attribute_name is right and not null.

What is session attribute in Spring MVC?

SessionAttribute annotation is the simplest and straight forward instead of getting session from request object and setting attribute. Any object can be added to the model in controller and it will stored in session if its name matches with the argument in @SessionAttributes annotation.

How do I remove a spring boot session?

You can disable Spring Session by setting the store-type to none . For setting the timeout of the session you can use the spring.

What does @SessionAttributes mean in spring?

@SessionAttribute annotation retrieve the existing attribute from the session. This annotation allows you to tell Spring which of your model attributes will also be copied to HttpSession before rendering the view.


1 Answers

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";
}
like image 150
michal.kreuzman Avatar answered Sep 22 '22 09:09

michal.kreuzman