前言
初学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></div>
下面是主要的测试代码
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";
}
}
</div>
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";
}
}
</div>
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.addAt

