前言

初学SpringMVC,最近在给公司做的系统做登录方面,需要用到session。

在网上找了不少资料,大致提了2点session保存方式:

1、javaWeb工程通用的HttpSession

2、SpringMVC特有的@SessionAttributes

我个人比较关注@SessionAttributes的用法,毕竟现在是在用SpringMVC嘛。但是我看网上那些文章,基本都是只说明了基础用法,详细的使用和细节却基本没有,我想这是不够的,所以我自己做了一些测试,然后整理了下代码做了个demo,记录并分享下,有什么不足的欢迎提出来讨论。

好了,废话就说到这,下面正戏开始!

结论

嗯,为了给一些不喜欢看代码的客官省去翻结论的麻烦,我这里就先把我测试后的结论先列一下吧。

1、可以通过SpringMVC特有的ModelMap、Model在Controller中自动保存数据到session,也可以通过传统的HttpSession等参数保存session数据

2、保存session数据必须使用@SessionAttributes注解,该注解有2种参数声明方式(value和types),且该注解声明必须写在类上,不能在方法上

3、保存的session数据必须与@SessionAttributes注解中的参数列表对应,未被声明的参数无法保存到session中

4、使用SessionStatus可以清除session中保存的数据,注意是全部清除,无法单独删除指定的session数据。同时,清除时有效权限遵循上述第2、3条规则(借用此规则可人为达到删除指定session数据的效果)

5、通过ModelMap等读取session中数据时,也有上述的参数权限限制

6、使用ModelMap或Model等保存session数据时,ModelMap必须作为方法参数传入,在方法中新定义的无效。同时,只要把ModelMap作为参数传入,即使是被别的方法调用也能起效

7、使用@ResponseBody注解时(一般配合ajax使用),无法保存session数据

8、@SessionAttributes注解可以使用value和types 2种参数列表

9、使用HttpSession的传统方式操作没有上述注解及权限等限制,下面有简单测试,但是不做具体说明

以下还有几个应该算是常识性的知识点

10、操作session数据可以跨类,与包或者url的路径等也没有关系

11、同一个session值操作,后面的值会覆盖前面的值

测试代码及简单说明

开发工具: Spring Tool Suite 。

spring专为SpringMVC搞出来的一款基于Eclipse的IDE开发工具,集成了Maven和Tomcat,最近用下来感觉还不错的,推荐下。

首先来一个项目结构截图吧

因为后面的测试中有用到ajax的@ResponseBody注解,所以要在pom.xml文件中配置jar包。

<!-- 使用@ResponseBody注解所需的2个包 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>

下面是主要的测试代码

 package test.dmh.session;

 import java.util.Enumeration;

 import javax.servlet.http.HttpSession;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus; /**
* @SessionAttributes 只声明了参数test1
*/
@Controller
@SessionAttributes(value={"test1"})
public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); @RequestMapping(value = "/show1")
public String show(ModelMap modelMap, HttpSession session) {
logger.info("show session");
for (Object key : modelMap.keySet()) {
Object value = modelMap.get(key);
System.out.println(key + " = " + value);
}
System.out.println("***********************************");
Enumeration<String> e = session.getAttributeNames();
while (e.hasMoreElements()) {
String s = e.nextElement();
System.out.println(s + " == " + session.getAttribute(s));
}
System.out.println("***********************************");
return "home";
} @RequestMapping("/set1")
public String setSession(ModelMap modelMap) {
logger.info("set session 1");
modelMap.addAttribute("test1", "value 1"); //设置一个在@SessionAttributes中声明过的参数
modelMap.addAttribute("test2", "value 2"); //设置一个未在@SessionAttributes中声明过的参数
return "home";
} @RequestMapping("/setM")
public String setSessionM(Model model) {
logger.info("set session 1");
model.addAttribute("test1", "value 1"); //设置一个在@SessionAttributes中声明过的参数
model.addAttribute("test2", "value 2"); //设置一个未在@SessionAttributes中声明过的参数
return "home";
} @RequestMapping("/clear1")
public String clear(SessionStatus status) {
logger.info("clear session 1");
status.setComplete();
return "home";
} }
 package test.dmh.session.controller;

 import javax.servlet.http.HttpSession;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; /**
* 没有使用@SessionAttributes注解
*/
@Controller
public class IndexController { private static final Logger logger = LoggerFactory.getLogger(IndexController.class); @RequestMapping("/set2")
public String setSession(ModelMap modelMap, HttpSession session) {
logger.info("set session 2 : without @SessionAttributes");
modelMap.addAttribute("test3", "value 3");
session.setAttribute("test4", "value 4");
return "home";
} }
 package test.dmh.session.controller;

 import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpSession; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus; @Controller
@SessionAttributes(value={"test5", "index"})
public class IndexController2 { private static final Logger logger = LoggerFactory.getLogger(IndexController2.class); @RequestMapping("/set3")
public String setSession(ModelMap modelMap, HttpSession session) {
logger.info("set session 3");
modelMap.addAttribute("test5", "value 5");
session.setAttribute("test6", "value 6"); ModelMap map = new ModelMap();
map.addAttribute("test7", "value 7"); this.setValueToSession(modelMap, session, "Hello World"); return "home";
} @ResponseBody
@RequestMapping(value="/login")
public Map<String, Object> login(ModelMap modelMap, HttpSession session) {
logger.info("login"); Map<String, Object> map = new HashMap<String, Object>(); map.put("success", true);
map.put("info", "登录成功!"); modelMap.addAttribute("testAjax", "test ajax value");
session.setAttribute("httpTestAjax", "http test ajax Value"); setValueToSession(modelMap, session, "This is Ajax"); return map;
} private void setValueToSession(ModelMap modelMap, HttpSession session, String value) {
logger.info("set session private");
modelMap.addAttribute("index", value);
session.setAttribute("httpIndex", value);
} @RequestMapping("/clear2")
public String clear(SessionStatus status) {
logger.info("clear session 2");
status.setComplete();
return "home";
} @RequestMapping(value = "/show2")
public String show(ModelMap modelMap, HttpSession session) {
logger.info("show session");
for (Object key : modelMap.keySet()) {
Object value = modelMap.get(key);
System.out.println(key + " = " + value);
}
System.out.println("***********************************");
Enumeration<String> e = session.getAttributeNames();
while (e.hasMoreElements()) {
String s = e.nextElement();
System.out.println(s + " == " + session.getAttribute(s));
}
System.out.println("***********************************");
return "home";
} }

这里如果也是跟我一样用STS建的项目,默认jsp文件会有配置<%@ page session="false" %>,一定要删除或者注释掉。否则无法在页面上展示session中的数据,当然通过我这边写的/show测试直接看后台代码也是可以的。

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1> <p> The test1 is ${sessionScope.test1}. </p>
<p> The test2 is ${sessionScope.test2}. </p>
<p> The test3 is ${sessionScope.test3}. </p>
<p> The test4 is ${sessionScope.test4}. </p>
<p> The test5 is ${sessionScope.test5}. </p>
<p> The test6 is ${sessionScope.test6}. </p>
<p> The test7 is ${sessionScope.test7}. </p>
<p> The index is ${sessionScope.index}. </p>
<p> The httpIndex is ${sessionScope.httpIndex}. </p> <br>
<input type="button" value="test" onclick="test();"> <script src="resources/js/jquery.min.js"></script>
<script type="text/javascript">
function test() {
$.ajax({
type : "POST",
url : "login",
dataType : "json",
success : function(data) {
console.log(data);
window.open("/session/test", "_self");
},
error : function() {
alert("出错了!");
}
});
}
</script> </body>
</html>

另外还有一个特别针对@SessionAttributes参数配置的测试代码

 package test.dmh.session.controller;

 import java.util.Enumeration;

 import javax.servlet.http.HttpSession;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus; @Controller
@SessionAttributes(value={"index1", "index2"}, types={String.class, Integer.class})
public class IndexController3 { private static final Logger logger = LoggerFactory.getLogger(IndexController3.class); @RequestMapping("/setIndex")
public String setSession(ModelMap modelMap) {
logger.info("set session index");
modelMap.addAttribute("index1", "aaa");
modelMap.addAttribute("index2", "bbb");
modelMap.addAttribute("index2", "ccc");
modelMap.addAttribute("DDD");
modelMap.addAttribute("FFF");
modelMap.addAttribute(22); return "home";
} @RequestMapping(value = "/showIndex")
public String show(ModelMap modelMap, HttpSession session) {
logger.info("show session");
for (Object key : modelMap.keySet()) {
Object value = modelMap.get(key);
System.out.println(key + " = " + value);
}
System.out.println("***********************************");
Enumeration<String> e = session.getAttributeNames();
while (e.hasMoreElements()) {
String s = e.nextElement();
System.out.println(s + " == " + session.getAttribute(s));
}
System.out.println("***********************************");
return "home";
} @RequestMapping("/clearIndex")
public String clear(SessionStatus status) {
logger.info("clear session index");
status.setComplete();
return "home";
} }

测试过程简单说明:

因为参数比较多,所以我也是懒得想名字,序列化的test1、2、3过去了。

测试的时候就是在浏览器上输入网址:http://localhost:8080/session/show1

然后把后缀show1改成别的,比如set1, set2以及clear1, clear2这些,具体的请看我代码中的@RequestMapping配置。

每次输入set1,set2这些以后,需要输入show1,show2来通过控制台查看session中的内容,当然直接在浏览器上看显示信息也是可以的。

这边我再说一下主要的几个结论:

1、使用ModelMap自动保存数据到session必须配置@SessionAttributes注解

2、使用@SessionAttributes注解只能声明在类上,声明以后,该类中的方法操作session数据只能对@SessionAttributes中配置的参数起作用,包括保存、清除和读取。

最后还有针对@SessionAttributes中的参数配置得出的几点结论:

1、配置参数提供value和types,存放的都是数组类型。(只有1个参数时不需要写成数组形式,比如@SessionAttributes(value="test1", types=Integer.class))

2、使用value配置参数类似于Map的键值对中的key

3、实用types配置参数后,后台保存的key就是它的类型,个人感觉只有在保存自定义类对象的时候有些用处,比如types=User.class,一般的常用类对象如String等我觉得还是用value的键值对比较好。当然,具体情况还是要具体分析的。

最后,感谢下在查找资料过程中我认为比较有用的博主提供的博文资料,有兴趣的同学也可以去看看。

Spring MVC中Session的正确用法之我见

Spring MVC @SessionAttributes注解


以上文章为本人根据官方及他人资料总结原创,仅供学习交流使用,代码实测可用。

最新文章

  1. UIScrollView的代理(delegate)
  2. inputType属性
  3. Activity的四种启动模式-图文并茂
  4. python简单网络服务器
  5. arm汇编指令总结(不断更新)
  6. BZOJ_1615_[Usaco2008_Mar]_The Loathesome_Hay Baler_麻烦的干草打包机_(模拟+宽搜/深搜)
  7. APP安全环节缺失,手游运营商怎样应对APP破解困境
  8. CSS3中的属性介绍
  9. python_如何实现可迭代对象和迭代器对象?
  10. 让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean
  11. Confluence 6 与其他应用整合
  12. CPU高速缓存行与内存关系 及并发MESI 协议
  13. check nginx配置文件错误:[emerg]: getpwnam(“nginx”) failed
  14. js事件监听
  15. MongoDB的客户端管理工具--nosqlbooster 查询工具使用
  16. 使用rsync 同步数据一些常用参数和示例
  17. Dependency injection configurations into views in asp.net core
  18. BoneBlack am335x利用SD卡烧写板卡上的emmc
  19. 【转发】JS中如何判断null/ undefined/IsNull
  20. [转] Elasticsearch 6.1官方入门教程

热门文章

  1. JavaWeb---javabean
  2. 设置node服务器的端口及运行环境
  3. 安卓Android的内存管理原理解析
  4. PHP后台程序员工作到如今的一点心得
  5. 爬取软考试题系列之ip自动代理
  6. 用Qemu搭建aarch32学习环境
  7. iOSiOS开发之数据存储之NSKeyedArchiver
  8. session的使用
  9. python学习随笔(二)
  10. iOS开发寻找最近公共view