【问题标题】:Spring MVC: How to remove session attribute?Spring MVC:如何删除会话属性?
【发布时间】:2013-08-15 01:51:36
【问题描述】:

下面使用@SessionAttributes 的示例。向导完成后如何清除user会话属性?在我的示例中返回/wizard0 后会话属性仍然存在。我试过status.setComplete()session.removeAttribute("user"),但是没用。

@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";
    }

}

编辑

我的错。 status.setComplete(); 效果很好。 session.removeAttribute("user") 在这里没什么可做的。

【问题讨论】:

  • status.setComplete(); 应该清理你的SessionAttribute。也许您正在将用户设置在另一个类中,或者您的会话配置有问题...
  • 您确认会话包含您添加的相同用户对象吗?在会话删除之前和之前检查用户对象的哈希码
  • 对不起。我的错误status.setComplete() 不过效果很好。我的完成按钮未正确映射-根本没有调用方法page4,所以这是个问题。我看到session.removeAttribute("user") / HttpSession@SessionAttributes无关。关闭/删除/等问题...再次抱歉。

标签: spring-mvc spring-3


【解决方案1】:

尝试使用WebRequest.removeAttribute 方法而不是HttpSession.setAttribute 方法(示例1)。或者另一种完全相同的方式,您可以使用“SessionAttributeStore.cleanupAttribute”(示例 2)。

示例 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";
}

示例 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";
}

【讨论】:

  • 如何实例化 SessionAttributeStore 因为我有期望:Could not instantiate bean class [org.springframework.web.bind.support.SessionAttributeStore]: Specified class is an interface.
  • 如何一次性清理所有会话属性?
【解决方案2】:

下面为我工作 -

@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";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 2017-07-08
    • 2021-02-20
    • 2019-09-16
    相关资源
    最近更新 更多