在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的使用情况的更多相关文章

  1. SpringMVC中session使用&&拦截器&&乱码处理&&异常处理

    ### 1. 使用Session 通常,会在Session中存放: 1. 客户端(用户)的身份标识,通常是用户的id:2. 使用频率非常高的数据,例如显示在页面中的用户名.头像等:3. 其它的不便于使 ...

  2. SpringMVC中session的使用

    SpringMVC中仍然可以使用传统方式使用session /** * 使用session - 传统方式 */ @RequestMapping("/hello13.action") ...

  3. SpringMVC从Session域中获取值

    SpringMVC从Session域中获取值 SpringMVC环境自行搭建 第一步:前端页面 第二步.后台代码 第三步.响应视图 第四步.在当前处理器所在的类设置@SessionAttributes ...

  4. springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序

    springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序 http://www.360doc.com/content/14/03 ...

  5. SpringMVC中的session用法及细节记录

    前言 初学SpringMVC,最近在给公司做的系统做登录方面,需要用到session. 在网上找了不少资料,大致提了2点session保存方式: 1.javaWeb工程通用的HttpSession 2 ...

  6. 9.springMVC中的拦截器

    springMVC中的拦截器大概大致可以分为以下几个步骤去学习: 1.自定义一个类实现HandlerInterceptor接口,这里要了解其中几个方法的作用 2.在springMVC的配置文件中添加拦 ...

  7. SpringMVC 中的Interceptor 拦截器

    1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors>  <!-- 日志拦截器 -->  <mvc:interceptor> ...

  8. 关于springMVC中component-scan的问题以及springmvc.xml整理

    关于springMVC中component-scan的问题以及springmvc.xml整理 一.component-scan问题和解决办法         最近在学习使用springMVC+myba ...

  9. SpringMVC中@Controller和@RequestMapping用法和其他常用注解

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

随机推荐

  1. Python3 小工具-TCP发现

    from scapy.all import * import optparse import threading import os def scan(ip): pkt=IP(dst=ip)/TCP( ...

  2. [leetcode-693-Binary Number with Alternating Bits]

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  3. [mongodb]child process failed, exited with error number 100

    Run the following command first to start the mongo server mongod run --config /usr/local/etc/mongod. ...

  4. 在linux下PHP和Mysql环境搞事情

    研发部门同事开发了一个接口管理辅助工具Shepherd,要求搭建在内网环境中,遇到点小问题记一下. 将开发的文件上传只web目录下,更改数据库ip,可以正常打开 登陆用户信息,此时需要连接数据库来验证 ...

  5. Alphabetic Removals(模拟水题)

    You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly ...

  6. “Hello world!”团队第二周贡献分规则+贡献分数分配结果

    一.贡献规则制定: (1)基础分:9 , 9 , 8 , 7 , 7 , 7 , 6(按在本次编程中承担模块的重要度制定,某一模块重要度的认定通过组内开会讨论决定) (2)会议分:每人没出勤一次会议记 ...

  7. (七)类、超类和子类 ——(多态,动态绑定,final类,类型转换,抽象类)

    java中所有的继承都是公有继承. 在子类中的构造其内可以初始化超类的公有域,但不能初始化超类的私有域. 因此需要在子类构造前的第一行使用super()语句初始化超类的私有域. 如果超类没有不带参数的 ...

  8. python实现post请求

    今天无论如何都要留下一些什么东西... 可以说今天学到一个新的一个东西,也需要分享出来,给更多的人去使用. 今天爬取的数据里面是客户端向服务器端发送加密过的token和一些页码之类的一个数据.(我主要 ...

  9. ZOJ 1666 G-Square Coins

    https://vjudge.net/contest/67836#problem/G People in Silverland use square coins. Not only they have ...

  10. centOS 6.5命令方式配置静态IP

    想自己做个centOS玩一下,然后通过FTP访问操作,首先查看是否开启了SSH,命令如下: rpm -qa | grep ssh 这个时候看到的是centOS的ssh已经打开!要是通过FTP工具访问还 ...