春季:@SessionAttributes与HttpSession [英] Spring: @SessionAttributes vs HttpSession

查看:302
本文介绍了春季:@SessionAttributes与HttpSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@SessionAttributes与HttpSession之间有什么区别? 两者中的哪一个会在会话中保留更多时间? 在哪种情况下,我必须使用一种,而在另一种情况下,则使用另一种?

What's difference between @SessionAttributes vs HttpSession? Which of the two, keeps for more time an object in the session? In which cases I have to use the one and in which cases the other?

谢谢

推荐答案

@SessionAttributes启用请求之间的持久模型属性会话,并且应根据具体情况来确定.目的是要提供一种构造,该构造将朝着实现对话范围(比会话短,比请求长)迈出一步. 博客.

@SessionAttributes enables persisting model attributes session between requests, and is meant to be specific per hanlder. The intent was to provide a construct that will be a step close towards implementing a conversation scope (shorter than session, longer than request). The need for conversational scope and why it is not fully available with @SessionAttributes is explained well in this blog.

它可以自动存储匹配的模型属性(匹配基于名称).默认存储为HttpSession,但是也可以使用其他方式进行配置.文档说

It enables the automatic storing of the matching model attributes (match is based on the name). The default storage is HttpSession but this can be configured differently also. The docs say

使用此批注指示的

会话属性对应于 特定处理程序的模型属性,以透明方式存储在 对话会话.这些属性将在 处理程序指示其会话会话已完成.

Session attributes as indicated using this annotation correspond to a specific handler's model attributes, getting transparently stored in a conversational session. Those attributes will be removed once the handler indicates completion of its conversational session.

但是该位一旦处理程序指示其对话会话已完成,这些属性将被删除.不会自动发生,开发人员可以通过使用<来指示退出对话在 SessionStatus 实例上的em> setComplete .否则,models属性将保留在会话中,这通常是不希望的副作用.

This bit however Those attributes will be removed once the handler indicates completion of its conversational session. does not happen automagically, and it is up to a developer to indicate the exit from the conversation by using setComplete on a SessionStatus instance. Otherwise the models attribute will remain in session, often an undesired side effect.

了解差异的最简单方法是观察模型变量,以@SessionAttribute为后盾的模型变量和正常" HttpSession变量的范围和值.

The easiest way to understand the difference is by observing the scope and the value of the model variable, model variable backed with @SessionAttribute, and the "normal" HttpSession variable.

看看两个简单的控制器

@Controller
@SessionAttributes("modelAndSession")
@RequestMapping("/sessionattr")
public class FirstController {
    protected static final String NEXT_VIEW = "next";
    @RequestMapping("/init")
    public String handlingMethod1( Model model, HttpSession session) {
        model.addAttribute(NEXT_VIEW, "/sessionattr/afterinit");
        session.setAttribute("session", "TRUE");
        model.addAttribute("modelAndSession", "TRUE");
        model.addAttribute("model", "TRUE");
        return "index";
    }

    @RequestMapping("/afterinit")
    public String handlingMethod2(SessionStatus status, Model model) {
        model.addAttribute(NEXT_VIEW, "/nosessionattr/init");
        //status.setComplete();
        return "index";
    }

}

第二个控制器

@Controller
@RequestMapping("/nosessionattr")
public class SecondController {
    protected static final String NEXT_VIEW = "next";
    @RequestMapping("/init")
    public String handlingMethod3(Model model) {
        model.addAttribute(NEXT_VIEW, "/sessionattr/init");
        return "index";
    }
}

以及将触发流程的视图

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<a href="${next}">Next step ${next}</a>
<hr/>
<table>
     <thead>
          <th>key</th> <th>Request scope</th> <th>Session scope</th>
     </thead>
    <tr>
        <td>model</td> <td>${requestScope.model}</td> <td>${sessionScope.model}</td>
    </tr>
    <tr>
        <td>model and session</td> <td>${requestScope.modelAndSession}</td> <td>${sessionScope.modelAndSession}</td>
    </tr>
    <tr>
        <td>session</td> <td>${requestScope.session}</td> <td>${sessionScope.session}</td>
    </tr>
</table>

流程

在初始请求/sessionattr/init上,视图呈现如下

The flow

Upon the initial request /sessionattr/init the view renders as follows

因此,模型变量在请求范围中可用,session属性在请求和会话范围中均可用,而正常"会话属性仅在会话范围中可用

so the model variable is available in the request scope, the sessionattribute is available in both the request and the session scope and the "normal" session attribute available only in session scope

在下一个请求/sessionattr/afterinit上,视图呈现如下

On the next request /sessionattr/afterinit the view renders as follows

因此,仅用于模型的变量消失了,而@SessionAttribute模型属性被从会话推送到模型并在请求中持久存在.下一步将定位到第二个控制器/nosessionattr/init,视图将呈现如下

so the model-only variable is gone, while the @SessionAttribute model attribute is pushed from the session to the model and persisted across requests. The next step will target a second controller /nosessionattr/init, and the view will render as follows

现在@SessionAttribute模型对象已从模型中删除,但是由于未显式调用status.setComplete,因此它仍作为常规变量保留在会话中

now the @SessionAttribute model object is gone from the model, but since the status.setComplete is not explicitely called it remained in the session as a normal variable

这是一个特别令人困惑的场景,因为许多人期望@SessionAttribute模型对象在切换处理程序之后应该消失,但是除非明确清除它,否则它将保留在会话中.随意复制代码片段,并进一步研究使您感到困惑的组合

This is a specially confusing scenario as many expect that @SessionAttribute model object should be gone after switching handler, however unless explicitly cleared it remains in the session. Feel free to copy the snippets and investigate further the combination that confuses you

这篇关于春季:@SessionAttributes与HttpSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆