[@Controller]6 详解@SessionAttributes
A、@SessionAttributes
org.springframework.web.bind.annotation.SessionAttributes
public @interface SessionAttributes
Annotation that indicates the session attributes that a specific handler uses. This will typically list the names of model attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans. Declared at the type level, applying to the model attributes that the annotated handler class operates on.
这个表明会话属性的注释被特定的处理器使用。它通常列出存储在会话中模型属性的名称。声明在类级别,适用于这个注释的处理器类可以操作的模型属性。
NOTE: Session attributes as indicated using this annotation correspond to a specific handler's model attributes, getting transparently stored in a conversational session. Those attributes will be removed once the handler indicates completion of its conversational session. Therefore, use this facility for such conversational attributes which are supposed to be stored in the session temporarily during the course of a specific handler's conversation.
注意:这个注释表明使用会话中的session就像使用一个特定的模型属性。一旦处理器完成这些属性也将被删除。因此使用这种会话属性的机制,都应支持在一个特定的会话过程中临时存储。
For permanent session attributes, e.g. a user authentication object, use the traditional session.setAttribute method instead. Alternatively, consider using the attribute management capabilities of the generic WebRequest interface.
永久会话属性(如:用户身份验证对象),请使用传统的session.setAttribute方法。另外,也可考虑使用WebRequest。
NOTE: When using controller interfaces (e.g. for AOP proxying), make sure to consistently put all your mapping annotations - such as @RequestMapping and @SessionAttributes - on the controller interface rather than on the implementation class.
注意,当时有Controller接口,确保始终把@RequestMapping和@SessionAttributes放在控制器接口上而不是实现类上。
属性
String[] value
The names of session attributes in the model, to be stored in the session or some conversational storage.
存储在会话中的会话属性名称。
Note: This indicates the model attribute names. The session attribute names may or may not match the model attribute names; applications should not rely on the session attribute names but rather operate on the model only.
注意,这里指定的模型属性名称。会话属性名称不一定匹配模型属性名称。应用程序不应该依赖会话属性名称。
Class[] types
The types of session attributes in the model, to be stored in the session or some conversational storage. All model attributes of this type will be stored in the session, regardless of attribute name.
存储在会话中的会话属性类型。这种类型的所有模型属性都将存储在会话中,无论属性名称是什么。
B、举例说明
范例1:通过Model绑定
Spring允许我们有选择地指定Model中的哪些属性需要转存到session中,以便下一个请求可通过Session来访问到这些属性。这一功能是通过类定义处标注@SessionAttributes注解来实现的。
@Controller
@RequestMapping(value = "login")
@SessionAttributes("mysession")
//定义把Model中的mysession属性的值绑定到Session中
public class LoginController {
@RequestMapping(method = RequestMethod.POST)
public String login(@ModelAttribute User user, ModelMap model) {
String viewName = "";
boolean check = true;
if (check) {
model.addAttribute("mysession", "123");
viewName = "redirect:/home";
} else {
viewName = "redirect:/";
}
return viewName;
}
}
这样我们不但可以在请求所对应的JSP视图页面中通过request.getAttribute()和session.getAttribute()获取mysession,还可以在下一个请求所对应的JSP视图页面中通过session.getAttribute()或ModelMap#get()访问到这个属性。
这里我们仅将一个ModelMap的属性放入Session中,其实@SessionAttributes允许指定多个属性。你可以通过字符串数组的方式指定多个属性,如 @SessionAttributes({“attr1”,”attr2”})。此外,@SessionAttributes还可以通过属性类型指定要 session化的ModelMap属性,如@SessionAttributes(types=User.class),当然也可以指定多个类,如 @SessionAttributes(types = {User.class,Dept.class}),还可以联合使用属性名和属性类型指定:@SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”})。
范例2:通过@ModelAttribute绑定
我们使用@ModelAttribute把表单自动绑定到对象上,那这个对象也可以通过@ModelAttribute(“”)绑定到Session中。
@Controller
@RequestMapping(value = "login")
@SessionAttributes("user")
//此处定义需要绑定到session中的model名称
public class LoginController {
@RequestMapping(method = RequestMethod.POST)
public String login(@ModelAttribute("user") User user, ModelMap model){
//@ModelAttribute将绑定到session中
String viewName = "";
boolean check = true;
if (check) {
viewName = "redirect:/home";
} else {
viewName = "redirect:/";
}
return viewName;
}
}
范例3:@SessionAttributes清除
@SessionAttributes需要清除时,使用SessionStatus.setComplete();来清除。注意,它只清除@SessionAttributes的session,不会清除HttpSession的数据。故如用户身份验证对象的session一般不同它来实现,还是用session.setAttribute等传统的方式实现。
@Controller
@RequestMapping(value = "login")
@SessionAttributes("mysession")
// 定义把Model中的mysession属性的值绑定到Session中
public class LoginController {
@RequestMapping(method = RequestMethod.POST)
public String login(@ModelAttribute User user, ModelMap model,
SessionStatus sessionStatus) {
String viewName = "";
boolean check = true;
if (check) {
model.addAttribute("mysession", "1233");
viewName = "redirect:/home";
} else {
viewName = "redirect:/";
}
sessionStatus.setComplete();
return viewName;
}
}