Java SpringMVC 中的 @SessionAttributes 什么时候被删除?(带代码示例)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1371392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When do @SessionAttributes in SpringMVC get removed? (With code sample)
提问by Daniel Alexiuc
Under what exact circumstances do @SessionAttributes get cleared? I've discovered some confusing behaviour when trying to use two models in a page.
@SessionAttributes 在什么确切情况下被清除?在尝试在页面中使用两个模型时,我发现了一些令人困惑的行为。
When I do a GET followed by a POST using this controller...
当我使用此控制器执行 GET 后跟 POST 时...
@Controller
@RequestMapping("/myPage*")
@SessionAttributes(value = {"object1", "object2"})
public class MyController {
@RequestMapping(method = RequestMethod.GET)
public String get(Model model) {
model.addAttribute("object1", new Object1());
model.addAttribute("object2", new Object2());
return "myPage";
}
@RequestMapping(method = RequestMethod.POST)
public String post(@ModelAttribute(value = "object1") Object1 object1) {
//do something with object1
return "myPage";
}
}
...object2 gets cleared from the Model. It no longer exists as a @SessionAttribute and cannot be accessed on my view page.
...object2 从模型中清除。它不再作为 @SessionAttribute 存在并且无法在我的视图页面上访问。
However if the signature of the second method is modified to this...
但是,如果将第二种方法的签名修改为此...
public String post(@ModelAttribute(value = "object1") Object1 object1,
@ModelAttribute(value = "object2") Object2 object2) {
...then object2 does not get cleared from the model and is available on my view page.
...然后 object2 不会从模型中清除并且在我的视图页面上可用。
The javadoc for @SessionAttributes says:
@SessionAttributes 的 javadoc 说:
... attributes will be removed once the handler indicates completion of its conversational session.
...一旦处理程序指示其会话会话完成,属性将被删除。
But I don't see how I have indicated completion of the conversational session in the first example but not in the second example.
但是我没有看到我是如何在第一个示例中而不是在第二个示例中指示会话会话的完成的。
Can anyone explain this behaviour or is it a bug?
任何人都可以解释这种行为还是一个错误?
采纳答案by ptomli
You indicate completion of the conversation by calling
你通过调用来表示对话的完成
SessionStatus.setComplete
SessionStatus.setComplete
public void post(...., SessionStatus status) {
status.setComplete();
}
That said, I don't see why you should be loosing one model attribute and not the other.
也就是说,我不明白为什么你应该失去一个模型属性而不是另一个。
Have you tried doing something like:
你有没有试过做这样的事情:
@ModelAttribute("object1")
public Object object1() { return new Object(); }
@ModelAttribute("object2")
public Object object2() { return new Object(); }
And see how that compares to putting the attributes in the model by hand.
看看这与手动将属性放入模型相比如何。
回答by Michaelzh
You can remove single session level ModelAttribute like this:
您可以像这样删除单个会话级别的 ModelAttribute:
Given ModelMap model
, HttpSession session
and you do:
鉴于ModelMap model
,HttpSession session
你做:
if (categoryId != null)
model.addAttribute("categoryId", categoryId);
else {
model.remove("categoryId");
session.removeAttribute("categoryId");
}