@SessionAttributes原理

  默认情况下Spring MVC将模型中的数据存储到request域中。当一个请求结束后,数据就失效了。如果要跨页面使用。那么需要使用到session。而@SessionAttributes注解就可以使得模型中的数据存储一份到session域中。

@SessionAttributes参数

  1、names:这是一个字符串数组。里面应写需要存储到session中数据的名称。

  2、types:根据指定参数的类型,将模型中对应类型的参数存储到session中

   3、value:其实和names是一样的。

Controller参考代码:

  

package com.tiekui.springmvc.handlers;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.tiekui.springmvc.pojo.Address;
import com.tiekui.springmvc.pojo.User; //http://www.cnblogs.com/caoyc/p/5635914.html
//只要是types中定义的类型,都会自动加入到sessionAttributes中。@SessionAttributes注解用于在类修饰中,而不是方法
@org.springframework.web.bind.annotation.SessionAttributes(value={"user"},types={Integer.class})
@Controller
public class SessionAttributes { @RequestMapping("testSessionAttributes")
public String testSessionAttributes(Map<String, Object> map) { User userTk = new User();
Address address = new Address();
address.setCity("city");
address.setProvince("province");
userTk.setAge(19);
userTk.setEmail("zhoutiekui@huawei.com");
userTk.setPassword("test");
userTk.setUsername("zhoutiekui");
userTk.setAddress(address); map.put("user", userTk);
map.put("age", 18);
map.put("count", 30); return "testSessionAttributes";
}
}

返回数据视图:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body> sessionAtrributes: ${sessionScope.user}
sessionAtrributes: ${sessionScope.age}
sessionAtrributes: ${sessionScope.count} </body>
</html>

调用视图:

<a href="testSessionAttributes">testSessionAttributes video 16</a>

本例中的添加到map的age和count都没有在SessionAtrributes的names/value值中,但是仍然可以被视SessionAttributes.

SpringMVC(十六) 处理模型数据之SessionAttributes的更多相关文章

  1. SpringMVC(十一):SpringMVC 处理输出模型数据之SessionAttributes

    Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...

  2. [WebGL入门]十二,模型数据和顶点属性

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外.鄙人webgl研究还不够深入,一些专业词语.假设翻译有误.欢迎大家指 ...

  3. SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息

    在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...

  4. WEB数据挖掘(十六)——Aperture数据抽取(9):数据源

    One of the central concepts of Aperture is the notion of a DataSource. A DataSource contains all inf ...

  5. SpringMVC系列(六)处理模型数据

    Spring MVC 提供了以下几种途径输出模型数据:  ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添加模型数据  Map 及 Model: ...

  6. SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute

    Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...

  7. SpringMVC(十):SpringMVC 处理输出模型数据之Map及Model

    Spring MVC提供了以下几种途径输出模型数据: 1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据: 2)Map及Model:处理方法 ...

  8. SpringMVC(十五) RequestMapping map模型数据

    控制器中使用map模型数据,传送数据给视图. 控制器参考代码: package com.tiekui.springmvc.handlers; import java.util.Arrays; impo ...

  9. SpringMvc:处理模型数据

    SpringMvc提供了以下途径输出模型数据: -ModelAndView:处理方法返回值类型为ModelAndView,方法体即可通过该对象添加模型数据 -Map或Model:入参为org.spri ...

随机推荐

  1. dell g3:安装ubuntu16.04 + CUDA8.0

    一.Ubuntu 1.选择UEFI模式启动,参考https://blog.csdn.net/qq_34570910/article/details/78084659 2.卡在logo界面进不去,在qu ...

  2. 断路器Feign

    Feign是自带断路器,需要在配置文件中开启断路器 改造消费者项目(FeignDemo) 1.在application.yml配置文件中开启断路器 eureka: client: service-ur ...

  3. css样式之补充。。。

    css常用的一些属性: 1.去掉下划线 :text-decoration:none ;2.加上下划线: text-decoration: underline; 3.调整文本和图片的位置(也就是设置元素 ...

  4. django之跨表查询及添加记录

    一:创建表 书籍模型: 书籍有书名和出版日期,一本书可能会有多个作者,一个作者也可以写多本书,所以作者和书籍的关系就是多对多的关联关系(many-to-many);     一本书只应该由一个出版商出 ...

  5. h5 video标签的使用

     标签的布置 <video src="1.mp4" poster="1.jpg" id="vid" controls> 你的浏览 ...

  6. C语言实现split以某个字符分割一个字符串

    方式一: 使用strtok # include <string.h> # include <stdio.h> void split(char *src,const char * ...

  7. Java 单字节、多字节读取文本文档中的内容

    文本文档位于工程下. 鼠标右击工程,选择“new - File”,即可创建. 文本文档的格式:GBK 单字节读取 import java.io.File; import java.io.FileInp ...

  8. Microsoft Visual Studio Community 2017 修改新建项目的默认位置

    IDE: Microsoft Visual Studio Community 2017 15.5.2 通过修改默认的设置,在下一次新建项目时,就可以节省一些不必要的操作. 菜单:工具 > 选项, ...

  9. stream to byte[], byte[] to srting

    byte[] myBinary = new byte[paramFile.Length]; paramFile.Read(myBinary, , (int)paramFile.Length); str ...

  10. HDU 1573 X问题(中国剩余定理标准解法)

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...