- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
@SessionAttributes和HttpSession有什么区别?
两者中的哪一个会在 session 中保留更多时间?
在哪些情况下我必须使用一种?在哪些情况下我必须使用另一种?
谢谢
最佳答案
@SessionAttributes允许在请求之间持久化模型属性 session ,并且应根据具体情况来确定。目的是要提供一种构造,该构造将朝着实现对话范围(比 session 短,比请求长)迈出一步。在blog中很好地解释了对话范围的需求以及为什么@SessionAttributes无法完全使用对话范围。
它可以自动存储匹配的模型属性(匹配基于名称)。默认存储为HttpSession,但是也可以使用其他方式进行配置。医生说
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.
@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
时, View 呈现如下
/sessionattr/afterinit
上, View 呈现如下
/nosessionattr/init
,该 View 将如下所示
关于 Spring :@SessionAttributes与HttpSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27191798/
我正在将 Javalin 设置为微服务,为我的 React 应用程序提供 API 端点。在本地,Javalin 在端口 7070 上运行,而 React 在端口 3000 上运行(通过带有 creat
我有一个 Spring Web 应用程序(Spring 3.2),我使用以下场景来处理编辑页面: @Controller @SessionAttributes(value = { "packet" }
我有一个 Spring-mvc 应用程序,在每个 Controller 中,我向 SessionAttributes 添加一个表单,以便在保存、删除或执行另一个获取请求时保留属性。当我尝试在另一个浏览
我正在使用 Spring MVC 3.0.5。我正在尝试学习如何使用@SessionAttributes。这是我的代码示例: @Controller @SessionAttributes("book"
如何使用 spring 框架和注释设置 session 属性? 相当于 request.getSession().setAttribute("key", "value"); 谢谢。 最佳答案 您可以
我怎样才能使 @SessionAttributes 限定在它们各自的 Controller 中,或者在过早地在 Controller 之间切换工作流时清理 @SessionAttributes? 示例
这是我的 Controller 代码:我已经将我的对象放在下面“doLogin”方法的映射中,我试图在我的“注销”函数中访问它但是当我尝试使用“map.get”获取我的 session 属性的值时我得
我正在尝试了解 Spring MVC 的架构。但是,我完全被 @SessionAttributes 的行为弄糊涂了。 请看下面的 SampleController,它是通过 SuperForm 类处理
我们正在使用最新版本的 spring。 . 我们正在使用 spring mvc 的 @Sessionattributes 在 session 范围内存储数据.. 问题是当我们使用浏览器的多个选项卡时,
我面临着一个特殊的问题。我正在学习 SpringMVC,这是我第一个使用它的应用程序项目。 当我们在 Controller 中使用@SessionAttributes注释时,如果调用任何具有与其签名中
我正在做一个简单的 Spring MVC 项目。有三个jsp页面第1页、第2页和第3页。在第1页和第2页中,我有用户表单,我想在第3页中打印两个用户的详细信息。我认为可以使用 spring mvc 注
几天来我一直在努力解决这个问题,它类似于 another question但那个答案并没有解决我的问题。 我无法从我的 Alexa 技能填充 JSON 响应的 sessionAttributes 属性
我在通过 session 保持电子邮件实时数据跨页时遇到问题我已经尝试过了 @Controller @RequestMapping(value = "/sec") @SessionAttributes
我尝试在我的 Controller 类中使用 @SessionAttributes,如下所示: @Controller @SessionAttributes({"calMethods", "taxLi
我正在使用 Spring。 每当用户登录时,我都会执行以下操作 model.addAttribute("user",user); 其中模型是对象 ModelMap model; 并将用户添加到 ses
我正在使用 2.5 并通过注释完成所有操作。 我有一个允许用户编辑对象的简单表单。它背后的 Controller 创建对象并在 GET 时将其添加到模型中,并在 POST(提交)时处理更改。它工作正常
我们正在为 80000 名用户构建一个 Spring-MVC 网络应用程序。 我在宠物诊所示例中看到很多 Controller 使用:@SessionAttribute 注释和SessionStatu
我有一个用于搜索和结果列表的抽象 Controller 支持类: @Controller @SessionAttributes("query") public abstract class Searc
我是 Node 和异步编程的新手,为了更好地理解回调,我在学习过程中尝试了所有能找到的方法。 这是 Lex 机器人与 Facebook 的集成,但我想我只是迷失在回调 hell 中。我的日志显示所有功
我最近正在尝试学习 Spring MVC。似乎我不太了解@SessionAttributes 和@ModelAttribute 注释的功能。 这是我的 Controller 的一部分: @Sessio
我是一名优秀的程序员,十分优秀!