@Controller的@sessionattributes
2015-08-10 09:37
405 查看
@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;
}
}
相关文章推荐
- C++常用术语及其英文翻译的含义和简单用途总结(六)
- 计蒜客 难题题库 126 PI值计算
- 【网络协议】互联网协议入门(二)
- Hibernate调优之select new
- Oracle数据库验证连接过不去的解决办法
- iOS 定时器使用详解
- 如何使用Heartbeat,组建一个高可用性的mysql集群
- db2使用指南及dbvis使用 和db2的卸载
- enum添加到combobox和获取
- 在RESTful中,遇到400 Bad Request
- HDU 1312 Red and Black
- 【日常学习】【背包DP】codevs1155 金明的预算方案题解
- 两棵树,你砍哪一棵?
- [Hibernate系列—] 3. 映射文件和使用SchemaExport制作自己主动Schema
- Distribution money
- Clojure发音
- 计蒜客 难题题库 125 删除字母'c'
- 为github帐号添加SSH keys
- Modis水汽数据MOD05去条带和转投影的问题
- Mac OS下配置远程Linux 服务器SSH密钥认证自动登录