@SessionAttributes只能应用在类上;
它用于将Map、ModelMap、Model或ModelAndView中的数据暂存到HttpSession中以使其可以在多个请求之间共用数据,注意它不支持HttpServletRequest
@SessionAttributes除了可以通过value或names注解属性指定哪些属性放到Session中以外,还可以通过type注解属性进行指定
下面来一个例子帮助理解
在index中定义两个a标签指向两个不同方法。
<a href="./userinfo/userInfoView.do">视图</a>
<a href="./userinfo/show.do">显示</a>
在对Controller类添加@SessionAttributes,可以看出show方法中没有任何操作。
@SessionAttributes("ui")
public class UserInfoController {
@RequestMapping("/userinfo/userInfoView.do")
public ModelAndView userInfoView() {
ModelAndView mv = new ModelAndView("userinfo/userInfoView");
UserInfo info = new UserInfo();
info.setName("laxi");
info.setMobile(110);
mv.addObject("ui", info);
return mv;
}
@RequestMapping("/userinfo/show.do")
public String show() {
return "userinfo/show";
}
}
两个不同的显示页面。
最后我们观察
在show.jsp中依然可以得到ui的值。