Spring MVC - 页面之间的变量,以及取消设置SessionAttribute

时间:2011-06-08 22:16:45

标签: java model-view-controller spring http session

这个问题听起来很奇怪,我正在玩Spring MVC并试图在两个页面之间移动,基本上我正在使用Spring Form JSTL创建一个JSP页面所以它只使用一个POST,我使用一个控制器来从一页移到下一页。但模型在页面之间丢失,我想隐藏实际变量,因此QueryStrings是不可能的(如 据我所知)。我知道我可以使用InternalResourceView,但只允许我使用模型。

我想传输一个对该页面是独占的变量,没有模型或使用QueryStrings的最佳方法是什么?

我计划使用SessionAttribute轻松定义它们,但是想知道,如何删除SessionAttribute创建的变量?我尝试了HttpSession.removeAttribute,它似乎没有用。

4 个答案:

答案 0 :(得分:5)

你也可以像这样使用SessionStatus.setComplete():

@RequestMapping(method = RequestMethod.GET, value="/clear")
public ModelAndView clear(SessionStatus status, ModelMap model, HttpServletRequest request) {
    model.clear();
    status.setComplete();
    return new ModelAndView("somePage");
}

或DefaultSessionAttributeStore.cleanUpAttribute像这样:

@RequestMapping(method = RequestMethod.GET, value="/clear")
public ModelAndView clear(DefaultSessionAttributeStore status, WebRequest request, ModelMap model) {
    model.remove("mySessionVar");
    status.cleanupAttribute(request, "mySessionVar");
    return new ModelAndView("somePage");
}

我在我的一个有多个sessionAttributes的表单上使用它,我想只删除其中一个。

答案 1 :(得分:3)

答案 2 :(得分:3)

您可以使用HttpSession类中的removeAttribute方法。

答案 3 :(得分:2)

您可以使用适用于Spring WebRequest.removeAttribute(String name, int scope)的{​​{1}}。引自@SessionAttributes javadoc - “或者,考虑使用通用{@link org.springframework.web.context.request.WebRequest}接口的属性管理功能。”

另见我的例子。

@SessionAttributes