在什么情况下@SessionAttributes被清除?当我尝试在页面中使用两个模型时,我发现了一些令人困惑的行为.
当我使用此控制器执行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";
}
}
Run Code Online (Sandbox Code Playgroud)
... object2从模型中清除.它不再作为@SessionAttribute存在,无法在我的视图页面上访问.
但是,如果第二种方法的签名被修改为......
public String post(@ModelAttribute(value = "object1") Object1 object1,
@ModelAttribute(value = "object2") Object2 object2) {
Run Code Online (Sandbox Code Playgroud)
...然后object2不会从模型中清除,并且在我的视图页面上可用.
@SessionAttributes的javadoc说:
...一旦处理程序指示其会话会话完成,将删除...属性.
但我没有看到我在第一个例子中表示完成了会话会话,但在第二个例子中没有表明.
任何人都可以解释这种行为或是一个错误吗?