简体   繁体   English

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

[英]Spring MVC - Variables between pages, and Unsetting a SessionAttribute

The question sounds weird, I'm playing around with Spring MVC and am trying to move between two pages and basically I'm creating a JSP page using Spring Form JSTL's so it just uses a POST, and I use a controller to move from one page to the next. The question sounds weird, I'm playing around with Spring MVC and am trying to move between two pages and basically I'm creating a JSP page using Spring Form JSTL's so it just uses a POST, and I use a controller to move from one页到下一页。 But Models are lost from page to page, and I'd like to hide the actual variable so QueryStrings are out of the question(as far as I know).但是模型从一页到另一页都丢失了,我想隐藏实际变量,所以 QueryStrings 是不可能的(据我所知)。 I know I can use a InternalResourceView, but only allows me to use a model.我知道我可以使用 InternalResourceView,但只允许我使用 model。

I want to transfer a variable that will be exclusive to that page, what's the best way without a model or using QueryStrings?我想传输一个该页面独有的变量,没有 model 或使用 QueryStrings 的最佳方法是什么?

I was planning on using SessionAttribute to easily define them, but was wondering, how do you remove a SessionAttribute created variable?我计划使用 SessionAttribute 轻松定义它们,但想知道如何删除 SessionAttribute 创建的变量? I tried HttpSession.removeAttribute and it didn't seem to work.我尝试了 HttpSession.removeAttribute ,但它似乎没有用。

You can also use SessionStatus.setComplete() like this:您还可以像这样使用 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");
}

or DefaultSessionAttributeStore.cleanUpAttribute like this:或 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");
}

I use it like this on one of my forms that has mulitple sessionAttributes and I want to remove only one of them.我在其中一个具有多个 sessionAttributes 的 forms 上使用它,我只想删除其中一个。

You can use the removeAttribute method from the HttpSession class.您可以使用 HttpSession class 中的removeAttribute方法。

you can use WebRequest.removeAttribute(String name, int scope) that works with Spring @SessionAttributes .您可以使用与 Spring @SessionAttributes一起使用的WebRequest.removeAttribute(String name, int scope) Quote from @SessionAttributes javadoc - "Alternatively, consider using the attribute management capabilities of the generic {@link org.springframework.web.context.request.WebRequest} interface."引用@SessionAttributes javadoc - “或者,考虑使用通用 {@link org.springframework.web.context.request.WebRequest} 接口的属性管理功能。”

Also look at my example.也看看我的例子。

@Controller
@SessionAttributes({"sessionAttr"})
public class MyController {

    @ModelAttribute("sessionAttr")
    public Object defaultSessionAttr() {
        return new Object();
    }

    @RequestMapping(value = "...", method = RequestMethod.GET)
    public String removeSessionAttr(WebRequest request, Model model) {
        request.removeAttribute("sessionAttr", WebRequest.SCOPE_SESSION);

        model.addAttribute("sessionAttr", defaultSessionAttr());
        return "myView";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM