下面的使用示例@SessionAttributes
。向导完成后如何清除user
会话属性?在我的示例中,返回/wizard0
会话属性后仍然存在。我试过了status.setComplete()
,session.removeAttribute("user")
但它不起作用。
@Controller
@SessionAttributes("user")
public class UserWizard {
@RequestMapping(value = "/wizard0", method = RequestMethod.GET)
public String page1(Model model) {
if(!model.containsAttribute("user")) {
model.addAttribute("user", new User());
}
return "wizard/page1";
}
@RequestMapping(value = "/wizard1", method = RequestMethod.GET)
public String page2(@ModelAttribute User user) {
user.setFirstname(Utils.randomString());
return "wizard/page2";
}
@RequestMapping(value = "/wizard2", method = RequestMethod.GET)
public String page3(@ModelAttribute User user) {
user.setLastname(Utils.randomString());
return "wizard/page3";
}
@RequestMapping(value = "/finish", method = RequestMethod.GET)
public String page4(@ModelAttribute User user, HttpSession session, SessionStatus status) {
/**
* store User ...
*/
status.setComplete();
session.removeAttribute("user");
return "redirect:/home";
}
}
编辑
我的错。status.setComplete();
效果很好。session.removeAttribute("user")
在这里没什么可做的。