添加使用session回话属性
@SessionAttributes("nowUser") nowUser :id/userName/password
public String delectMsg(int id, int num, Model model, @ModelAttribute("nowUser") User user) {}
调用方法时候,传了个id值,这时候,会修改nowUser里面的id值。。。。。。。。。。。。
一、@SessionAttributes
在默认情况下,ModelMap 中的属性作用域是 request 级别是,也就是说,当本次请求结束后,ModelMap中的属性将销毁。如果希望在多个请求中共享 ModelMap 中的属性,必须将其属性转存到 session 中,这样ModelMap 的属性才可以被跨请求访问。
spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。
使模型对象的特定属性具有 Session 范围的作用域
@Controller
@RequestMapping("/bbtForum.do")
@SessionAttributes("currUser") //①将ModelMap中属性名为currUser的属性
//放到Session属性列表中,以便这个属性可以跨请求访问
public class BbtForumController {
…
@RequestMapping(params = "method=listBoardTopic")
public String listBoardTopic(@RequestParam("id")int topicId, User user,
ModelMap model) {
bbtForumService.getBoardTopics(topicId);
System.out.println("topicId:" + topicId);
System.out.println("user:" + user);
model.addAttribute("currUser",user); //②向ModelMap中添加一个属性
return "listTopic";
} }
我们在 ② 处添加了一个 ModelMap 属性,其属性名为 currUser,而 ① 处通过 @SessionAttributes 注解将 ModelMap 中名为 currUser 的属性放置到 Session 中,所以我们不但可以在 listBoardTopic() 请求所对应的 JSP 视图页面中通过 request.getAttribute(“currUser”) 和 session.getAttribute(“currUser”) 获取 user 对象,还可以在下一个请求所对应的 JSP 视图页面中通过 session.getAttribute(“currUser”) 或ModelMap#get(“currUser”) 访问到这个属性。
这里我们仅将一个 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”})。
二、@ModelAttribute
我们可以在需要访问 Session 属性的 controller 上加上 @SessionAttributes,然后在 action 需要的 User 参数上加上 @ModelAttribute,并保证两者的属性名称一致。SpringMVC 就会自动将 @SessionAttributes 定义的属性注入到 ModelMap 对象,在 setup action 的参数列表时,去 ModelMap 中取到这样的对象,再添加到参数列表。只要我们不去调用 SessionStatus 的 setComplete() 方法,这个对象就会一直保留在 Session 中,从而实现 Session 信息的共享。
@Controller
@SessionAttributes("currentUser")
public class GreetingController{
@RequestMapping
public void hello(@ModelAttribute("currentUser") User user){
//user.sayHello()
}
}
A、@SessionAttributes
org.springframework.web.bind.annotation.SessionAttributes
public @interface SessionAttributes
这个表明会话属性的注释被特定的处理器使用。它通常列出存储在会话中模型属性的名称。声明在类级别,适用于这个注释的处理器类可以操作的模型属性。
注意:这个注释表明使用会话中的session就像使用一个特定的模型属性。一旦处理器完成这些属性也将被删除。因此使用这种会话属性的机制,都应支持在一个特定的会话过程中临时存储。
永久会话属性(如:用户身份验证对象),请使用传统的session.setAttribute方法。另外,也可考虑使用WebRequest。
注意,当时有Controller接口,确保始终把@RequestMapping和@SessionAttributes放在控制器接口上而不是实现类上。
属性
String[] value
存储在会话中的会话属性名称。
注意,这里指定的模型属性名称。会话属性名称不一定匹配模型属性名称。应用程序不应该依赖会话属性名称。
Class[] types
存储在会话中的会话属性类型。这种类型的所有模型属性都将存储在会话中,无论属性名称是什么。
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;
}
}
添加使用session回话属性的更多相关文章
- struct2访问或添加request/session/application
访问或添加request/session/application 1 通过ActionContext //这样放置 public String execute() { ActionConte ...
- Session的属性
Session的属性 Session在网络应用中被称为会话.具体到web中的Session指的就是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间,因 ...
- 为普通Object添加类似AttachedProperty的属性
为普通Object添加类似AttachedProperty的属性 周银辉 我们知道,在WPF中对应一个DependencyObject,我们很容易通过AttachedProperty来为类型附加一 ...
- jquery的2.0.3版本源码系列(3):96行-283行,给JQ对象,添加一些方法和属性
jquery是面向对象的程序,面向对象就离不开方法和属性. 方法的简化 jQuery.fn=jQuery.prototype={ jquery: 版本 constructor: 修正指向问题 init ...
- Android自定义视图一:扩展现有的视图,添加新的XML属性
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- Asp.Net WebApi开启Session回话
一.在WebApi项目中默认没有开启Session回话支持.需要在Global中的Init()方法中指定会员需要支持的类型 public class WebApiApplication : Syste ...
- ArcGIS Engine效率探究——要素的添加和删除、属性的读取和更新
ArcGIS Engine效率探究——要素的添加和删除.属性的读取和更新 来自:http://blog.csdn.net/freewaywalker/article/details/23703863 ...
- jQuery源码06-jQuery = function(){};给JQ对象,添加一些方法和属性,extend : JQ的继承方法,jQuery.extend()
/*! * Includes Sizzle.js 选择器,独立的库 * http://sizzlejs.com/ */ (function( window, undefined ) { //" ...
- 使用Typescript重构axios(二十六)——添加HTTP授权auth属性
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
随机推荐
- 【C++】函数和指针
最近在看C++ primer plus,感觉函数与指针这一章难点比较多,记写笔记,加强理解. From C++ Primer Plus: Chapter 7 Function:C++ Programm ...
- 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
思路大概和其他的题解一样: 从当前字符串最前面,最后面选一个字典序较小的然后拉到一个新的字符串序列中,如果相同就一直往中间扫描直到发现不同为止(一个字符如果被选中之后那么就不可以再次选择了),所以我们 ...
- 【codeforces 816C】Karen and Game
[题目链接]:http://codeforces.com/contest/816/problem/C [题意] 给你一个n*m的矩阵; 一开始所有数字都是0; 每次操作,你能把某一行,或某一列的数字全 ...
- [MySQL]快速解决"is marked as crashed and should be repaired"故障[转]
Table '.\Tablename\posts' is marked as crashed and should be repaired 提示说论坛的帖子表posts被标记有问题,需要修复.我记得以 ...
- java 基本类型、包装类、字符串之间的转换
1.基本类型和包装类 基本类型和包装类可通过自动装箱和拆箱实现. int i = 24; Integer a = new Integer(i); //手动装箱 Integer b = i; //自动装 ...
- POJ 2516 Minimum Cost (最小费用最大流)
POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...
- Android 经常使用工作命令mmm,mm,m,croot,cgrep,jgrep,resgrep,godir
官方定义: Invoke ". build/envsetup.sh" from your shell to add the following functions to your ...
- nfs共享文件服务搭建
网络文件共享服务器192.10.19.132yum install -y nfs-utils 在exports文件中添加的从机范围vim /etc/exports/home/nfs/ 192.10.1 ...
- [NOIP2015模拟10.22] 最大子矩阵 解题报告(单调栈)
Description 我们将矩阵A中位于第i行第j列的元素记作A[i,j].一个矩阵A是酷的仅当它满足下面的条件: A[1,1]+A[r,s]<=A[1,s]+A[r,1](r,s ...
- CheckException和RuntimeException
java文档中对RuntimeException的定义是: RuntimeException 是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类. 可能在执行方法期间抛出但未被捕获的 Runt ...