转载一篇关于springmvc下session的用法,觉得作者写的不错,给大家分享一下
来源:互联网 发布:淘宝上李宁官方网店 编辑:程序博客网 时间:2024/05/21 22:24
绑定模型对象中某个属性
Spring 2.0 定义了一个 org.springframework.ui.ModelMap 类,它作为通用的模型数据承载对象,传递数据供视图所用。我们可以在请求处理方法中声明一个 ModelMap 类型的入参,Spring 会将本次请求模型对象引用通过该入参传递进来,这样就可以在请求处理方法内部访问模型对象了。来看下面的例子:
使用 ModelMap 访问请示对应的隐含模型对象
@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); //① 将user对象以currUser为键放入到model中 model.addAttribute("currUser",user); return "listTopic"; }
对于当次请求所对应的模型对象来说,其所有属性都将存放到 request 的属性列表中。象上面的例子,ModelMap 中的 currUser 属性将放到 request 的属性列表中,所以可以在 JSP 视图页面中通过 request.getAttribute(“currUser”) 或者通过 ${currUser} EL 表达式访问模型对象中的 user 对象。从这个角度上看, ModelMap 相当于是一个向 request 属性列表中添加对象的一条管道,借由 ModelMap 对象的支持,我们可以在一个不依赖 Servlet API 的 Controller 中向 request 中添加属性。 在默认情况下,ModelMap 中的属性作用域是 request 级别是,也就是说,当本次请求结束后,ModelMap 中的属性将销毁。如果希望在多个请求中共享 ModelMap 中的属性,必须将其属性转存到 session 中,这样 ModelMap 的属性才可以被跨请求访问。
Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。请看下面的代码:
使模型对象的特定属性具有 Session 范围的作用域
package com.baobaotao.web;…import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.SessionAttributes;@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”})。
上面讲述了如何往ModelMap中放置属性以及如何使ModelMap中的属性拥有Session域的作用范围。除了在JSP视图页面中通过传统的方法访问ModelMap中的属性外,读者朋友可能会问:是否可以将ModelMap中的属性绑定到请求处理方法的入参中呢?答案是肯定的。Spring为此提供了一个@ModelAttribute的注解,下面是使用@ModelAttribute注解的例子:
使模型对象的特定属性具有 Session 范围的作用域
package com.baobaotao.web;import com.baobaotao.service.BbtForumService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.SessionAttributes;import org.springframework.web.bind.annotation.ModelAttribute;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;@Controller@RequestMapping("/bbtForum.do")@SessionAttributes("currUser") //①让ModelMap的currUser属性拥有session级作用域public class BbtForumController { @Autowiredprivate BbtForumService bbtForumService; @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"; } @RequestMapping(params = "method=listAllBoard") //③将ModelMap中的public String listAllBoard(@ModelAttribute("currUser") User user) { //currUser属性绑定到user入参中。 bbtForumService.getAllBoard(); System.out.println("user:"+user); return "listBoard"; }}
在 ② 处,我们向 ModelMap 中添加一个名为 currUser 的属性,而 ① 外的注解使这个 currUser 属性拥有了 session 级的作用域。所以,我们可以在 ③ 处通过 @ModelAttribute 注解将 ModelMap 中的 currUser 属性绑定以请求处理方法的 user 入参中。所以当我们先调用以下 URL 请求: http://localhost/bbtForum.do?method=listBoardTopic&id=1&userName=tom&dept.deptId=12
以执行listBoardTopic()请求处理方法,然后再访问以下URL: http://localhost/sample/bbtForum.do?method=listAllBoard
你将可以看到 listAllBoard() 的 user 入参已经成功绑定到 listBoardTopic() 中注册的 session 级的 currUser 属性上了。
注明:这里是原文的网址https://my.oschina.net/rouchongzi/blog/161871
阅读全文
0 0
- 转载一篇关于springmvc下session的用法,觉得作者写的不错,给大家分享一下
- 看到的一篇文章 觉得不错 转载....
- 最近在看反射,觉得写的还不错,分享一下
- 一次偶然机遇找到一个不错的关于Android实现加载gif动画的实例,写一下分享个大家。
- 给大家推荐一下不错的博客
- 一个个人觉得比较好用的PHP日志类,给大家分享一下
- 朋友发了一篇关于接口跟抽象类的文章,觉得不错收藏一下
- 在网上看到一篇文章,挺不错的,与大家分享一下
- (最近学习过程中发现的,觉得不错,和大家分享一下)MFC中OnDraw与OnPaint的区别
- 转载一篇个人觉得不错的关于对象引用的文章
- 一篇演讲稿,不错,给大家推荐一下。看人家怎么忽悠的
- 转载一篇觉得不错的文章,关于并发访问资源共享问题
- 在百度看到的一篇关于程序员的文章,觉得挺好,转载一下
- 博主发声:你期望的限免,告诉我们 (这不是广告,只是觉得不错,想分享给大家!)
- [分享]觉得不错的网站
- Node入门 (转载)--个人觉得写的不错,赞!
- 给大家分享一下项目中基于springMVC基于注解部分的xml配置文件,很实用
- 今天看了关于重载与多态的文章,觉得不错,分享下
- 还是A+B
- VLC支持的视频和音频文件扩展名
- 深入理解JavaScript
- D
- Caffe环境的准备
- 转载一篇关于springmvc下session的用法,觉得作者写的不错,给大家分享一下
- 7css3的前缀及盒模型
- 前m大的数
- DIV+CSS实现商城首页
- hdu 1078 fatmouse and cheese
- centos系统安装redis4.0(单机)
- ActionBar的应用
- Python学习整理之 列表list 元组tuple 字典dictionary
- 元素出栈、入栈顺序的合法性