如果我忘记将 Spring SessionStatus 标记为“Complete"会怎样? [英] What happens if I forget to mark the Spring SessionStatus as "Complete"?

查看:28
本文介绍了如果我忘记将 Spring SessionStatus 标记为“Complete"会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Spring MVC 中,假设我使用 @SessionAttribute 标签定义了一个 SessionAttribute,如下所示:

In Spring MVC, suppose I define a SessionAttribute, using the @SessionAttribute tag like so:

@SessionAttributes(value = "myModel")
public class MyController{
   ...
}

假设我忘记像这样在 SessionStatus 上调用 status.setComplete():

Suppose that I forget to call status.setComplete() on the SessionStatus like so:

@RequestMapping(method = RequestMethod.POST)
public void doSomething(@ModelAttribute("myModel") MyModel model, SessionStatus status){
   ...
   //status.setComplete(); <-- Never gets called
}

模型会永远留在会话中吗?它会被清理掉,还是会随着用户浏览网站而变得越来越大?

Will the model stay in the session forever? Will it ever get cleaned out, or will the session keep growing larger and larger as the user navigates the site?

推荐答案

编辑 #2:请注意,此答案不再正确.请参阅下面的 @doanduyhai 的回答.

EDIT #2: Note that this answer is no longer correct. See @doanduyhai's answer below.

请注意,这适用于 Spring 2.5,并且可能,但不一定确保 Spring 3.x 也是如此.仔细检查文档!

Please note that this is for Spring 2.5 and may, but does not necessarily ensure that the same is for Spring 3.x. Double check the docs!

这与@Gandalf 所说的一致.

This is along the lines of what @Gandalf said.

表单控制器对表单请求的生命周期进行建模,从最初查看表单到提交表单.表单提交后,表单控制器的工作就完成了,它将从会话中移除命令对象.

Form controllers model a form request lifespan, from initial viewing of the form through form submission. After the form is submitted, the form controller's job is done, and it will remove the command object from the session.

因此,要将命令对象保留在表单工作流之间的会话中,您需要手动管理会话.在干净的 POST 之后,该对象将从会话中删除.

So, to keep the command object in the session between form workflows you will need to manage the session manually. After a clean POST, the object is removed from session.

简而言之,我认为 setComplete() 方法只是一种很好的做法,但不是必需的.

In short, I believe the setComplete() method is just good practice but is not necessarily required.

编辑:我刚刚查看了我的 Spring book 以确认这一点.我会引用它:

EDIT: I just looked in my Spring book to confirm this. I'll quote it:

当不使用@SessionAttribute 时,一个将创建新的命令对象每个请求,即使在呈现由于装订错误而再次形成.如果此注释已启用,则命令对象将存储在供后续使用的会话,直到表格成功完成.然后此命令对象将被清除来自会话. 这通常是当命令对象是需要的持久对象在不同的请求中相同用于跟踪更改.

When @SessionAttribute is not used, a new command object will be created on each request, even when rendering the form again due to binding errors. If this annotation is enabled, the command object will be stored in the session for subsequent uses, until the form completes successfully. Then this command object will be cleared from the session. This is usually used when the command object is a persistent object that needs to be identical across different requests for tracking changes.

基本上就是我上面所说的.它将它存储在会话中,直到您 A) 调用 setComplete() 或 B) 控制器成功完成 POST.

Essentially that's what I was saying above. It stores it in the session until you either A) call setComplete() or B) the controller successfully completes a POST.

这篇关于如果我忘记将 Spring SessionStatus 标记为“Complete"会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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