资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

session如何在SpringMVC项目中使用

session如何在SpringMVC项目中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、网络空间、营销软件、网站建设、山阴网站维护、网站推广。

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,最近用下来感觉还不错的,推荐下。

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

session如何在SpringMVC项目中使用

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



 org.codehaus.jackson
 jackson-core-asl
 1.9.13


 org.codehaus.jackson
 jackson-mapper-asl
 1.9.13

下面是主要的测试代码

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 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 login(ModelMap modelMap, HttpSession session) {
  logger.info("login");
  
  Map map = new HashMap();
  
  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 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" %>
4 

 Home


Hello world!

The test1 is ${sessionScope.test1}.

The test2 is ${sessionScope.test2}.

The test3 is ${sessionScope.test3}.

The test4 is ${sessionScope.test4}.

The test5 is ${sessionScope.test5}.

The test6 is ${sessionScope.test6}.

The test7 is ${sessionScope.test7}.

The index is ${sessionScope.index}.

The httpIndex is ${sessionScope.httpIndex}.


另外还有一个特别针对@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 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的键值对比较好。当然,具体情况还是要具体分析的。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


本文标题:session如何在SpringMVC项目中使用
文章位置:http://cdkjz.cn/article/jpdpdi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220