SpringMVC中 Session的使用情况
在SpringMVC中,使用Session可以用通过两种方式
1、servlet-api 即HttpSession
session.setAttritute(),session.getAttribute();
2、使用@sessionAttributes()注解
①Spring框架会在调用完Controller之后、渲染View之前检查Model的信息,并把@SessionAttributes()注释标明的属性加入session中
②@ModelAttribute在声明Controller的参数的时候,可以用来表明此参数引用某个存在在Model中的对象,如果 这个对象已经存在于Model中的话(Model可以在调用Controller之前就已经保存有数据,这应该不仅仅因为 HandlerInterceptor或者@ModelAttribute标记的方法已经显式的将一些对象加入到了Model对象中,也因为Spring会默认将一些对象加入到Model中,这一点很重要)。
③如果Session中已经存在某个对象,那么可以直接使用ModelAttribute声明Controller的参数,在Controller中可以直接使用它。
eg.1
@Controller
public class ManagerController { @RequestMapping(value = "/login",method = RequestMethod.GET)
public ModelAndView login(HttpSession httpSession){ httpSession.setAttribute("username", "admin");
return new ModelAndView("login");
}
@RequestMapping(value = "/logout",method = RequestMethod.GET)
public String logout(HttpSession httpSession){
String name=httpSession.getAttribute("username");
return "success";
}
eg.2
@Controller
@SessionAttributes("username")
public class ManagerController { @RequestMapping(value = "/login",method = RequestMethod.GET)
public ModelAndView login(Model model)
model.addAttribute("username", "admin");
return new ModelAndView("login"); } @RequestMapping(value = "/logout",method = RequestMethod.GET)
public String logout(@ModelAttribute("username") String name){
System.out.println(name);
return "success"; } }
参考链接:http://www.cnblogs.com/waytofall/p/3460533.html
SpringMVC中 Session的使用情况的更多相关文章
- SpringMVC中session使用&&拦截器&&乱码处理&&异常处理
### 1. 使用Session 通常,会在Session中存放: 1. 客户端(用户)的身份标识,通常是用户的id:2. 使用频率非常高的数据,例如显示在页面中的用户名.头像等:3. 其它的不便于使 ...
- SpringMVC中session的使用
SpringMVC中仍然可以使用传统方式使用session /** * 使用session - 传统方式 */ @RequestMapping("/hello13.action") ...
- SpringMVC从Session域中获取值
SpringMVC从Session域中获取值 SpringMVC环境自行搭建 第一步:前端页面 第二步.后台代码 第三步.响应视图 第四步.在当前处理器所在的类设置@SessionAttributes ...
- springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序 http://www.360doc.com/content/14/03 ...
- SpringMVC中的session用法及细节记录
前言 初学SpringMVC,最近在给公司做的系统做登录方面,需要用到session. 在网上找了不少资料,大致提了2点session保存方式: 1.javaWeb工程通用的HttpSession 2 ...
- 9.springMVC中的拦截器
springMVC中的拦截器大概大致可以分为以下几个步骤去学习: 1.自定义一个类实现HandlerInterceptor接口,这里要了解其中几个方法的作用 2.在springMVC的配置文件中添加拦 ...
- SpringMVC 中的Interceptor 拦截器
1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> ...
- 关于springMVC中component-scan的问题以及springmvc.xml整理
关于springMVC中component-scan的问题以及springmvc.xml整理 一.component-scan问题和解决办法 最近在学习使用springMVC+myba ...
- SpringMVC中@Controller和@RequestMapping用法和其他常用注解
一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...
随机推荐
- Bcp 使用心得【转】
在做这方面研究的时候,的确遇到了不少麻烦. 首先在做bcp的时候,要开通大数据量访问权限 一.基于sql语句的导入导出 如果是基于SQL语句的导入导出,需要使用存储过程“master..xp_cmds ...
- Appium基础环境搭建(windows)---基于python
1 JDK安装 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 安装注意:安装 ...
- 定点数(fixed-point number)
定义 定点数(fixed-point number)就是小数点位置固定的数,也就是说,小数点后面的位数是固定的,比如要记录一笔账目,这些账目的数字都不会超过100,就可以使用2位小数位定点数来记录,比 ...
- WPF+数据库+三层
1.计算类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...
- TCP系列27—窗口管理&流控—1、概述
在前面的内容中我们依次介绍了TCP的连接建立和终止过程和TCP的各种重传方式.接着我们在这部分首先关注交互式应用TCP连接相关内容如延迟ACK.Nagle算法.Cork算法等,接着我们引入流控机制(f ...
- asp.net 后台注册(调用)JS
1.使用Page.ClientScript.RegisterClientScriptBlock 使用 Page.ClientScript.RegisterClientScriptBlock可以防止ja ...
- MDK中的调试脚本分析
准备写一个简单的裸机程序放mini2440里面跑,看到芯片手册有2中启动方式,1.从Nor Flash启动 2.从Nand Flash中启动.断断续续弄了几天始终无法烧录进去,想起平时自己工作中调试程 ...
- 3dContactPointAnnotationTool开发日志(二四)
添加了清空2d接触点的按钮,将输出的2d接触点的单位变成了像素,原点在图像的左下角. 对于obj文件的适配更加多样化了.
- Thread.Sleep(0)
理解Thread.Sleep函数 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢? 思考下面这两个问题: 1.假设现在是 2008-4- ...
- 求助 delphi ADO组件的 CursorLocation属性设置为 clUseServer 用法 [问题点数:20分]
我有个管理系统,所有ADOQUERY组件的 CursorLocation属性设置为 clUseClient,一直运行正常,我尝试全部设置为clUseServer, 系统不运行了,请大家帮忙. 我的做法 ...