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 ...
随机推荐
- sqlserver 2008 merger语句
Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...
- Jamie and Alarm Snooze
Description Jamie loves sleeping. One day, he decides that he needs to wake up at exactly hh: mm. Ho ...
- 20145214 《Java程序设计》第4周学习总结
20145214 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承基本上就是避免多个类间重复定义共同行为.要避免在程序设计上出现重复,可以把相同的程序代码提升为父类. 关键 ...
- jQuery动态添加li标签并添加属性和绑定事件
代码如下: <%@page import="java.util.ArrayList"%> <%@ page language="java" c ...
- 【week2】Scrum中的站立会议
Scrum站立会议 站立会议给我的第一印象就是站着开会,在经过我查阅资料之后,发现也是差不多的意思.学术一点的分析就是在Sprint开始后,团队将会在每个工作日特定时间举行一个简短会议,每次会议 ...
- Nautilus-Share-Message: Called "net usershare info" but it failed: Failed to
See what nautilus processes are running : ps aux | grep nautilus Kill all nautilus processes you see ...
- do_try_to_free_pages
/* * This is the main entry point to direct page reclaim. * * If a full scan of the inactive list fa ...
- wpf 验证方法
效果图,当放鼠标到文本框上会显示出错的提示.
- 【SQLAlchemy】SQLAlchemy修改查询字段列名
SQLAlchemy问题记录 company price quantity Microsoft Google Google Google 要实现脚本 select price, sum(quantit ...
- 2018 杭电多校2 - Naive Operations
题目链接 Problem Description In a galaxy far, far away, there are two integer sequence a and b of length ...