SpringMVC(十六) 处理模型数据之SessionAttributes
@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的更多相关文章
- SpringMVC(十一):SpringMVC 处理输出模型数据之SessionAttributes
Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...
- [WebGL入门]十二,模型数据和顶点属性
注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外.鄙人webgl研究还不够深入,一些专业词语.假设翻译有误.欢迎大家指 ...
- SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息
在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...
- WEB数据挖掘(十六)——Aperture数据抽取(9):数据源
One of the central concepts of Aperture is the notion of a DataSource. A DataSource contains all inf ...
- SpringMVC系列(六)处理模型数据
Spring MVC 提供了以下几种途径输出模型数据: ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添加模型数据 Map 及 Model: ...
- SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute
Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...
- SpringMVC(十):SpringMVC 处理输出模型数据之Map及Model
Spring MVC提供了以下几种途径输出模型数据: 1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据: 2)Map及Model:处理方法 ...
- SpringMVC(十五) RequestMapping map模型数据
控制器中使用map模型数据,传送数据给视图. 控制器参考代码: package com.tiekui.springmvc.handlers; import java.util.Arrays; impo ...
- SpringMvc:处理模型数据
SpringMvc提供了以下途径输出模型数据: -ModelAndView:处理方法返回值类型为ModelAndView,方法体即可通过该对象添加模型数据 -Map或Model:入参为org.spri ...
随机推荐
- Java的家庭记账本程序(A)
日期:2019.2.1 博客期:028 星期五 其实我早就开始开发“家庭记账本”的软件了,只不过写博客写的有点晚,我是打算先做web的!因为Android Studio的教程,还是要对应版本,好多问题 ...
- selenium怎么操作web页面常见的元素
总结一下selenium怎么操作web页面常见的元素. 主要有: 上传 alter dialog prompt dialog confirm dialog select list radio box ...
- CentOS下将Python的版本升级为3.x
本文主要介绍在Linux(CentOS)下将Python的版本升级为3.x的方法 众所周知,在2020年python官方将不再支持2.7版本的python,所以使用3.x版本的python是必要的,但 ...
- C++ friend友元函数和友元类(转)
一个类中可以有 public.protected.private 三种属性的成员,通过对象可以访问 public 成员,只有本类中的函数可以访问本类的 private 成员.现在,我们来介绍一种例外情 ...
- 论文阅读笔记二十:LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation(CVPR2017)
源文网址:https://arxiv.org/abs/1707.03718 tensorflow代码:https://github.com/luofan18/linknet-tensorflow 基于 ...
- Hadoop数据分析平台项目实战(基于CDH版本集群部署与安装)
1.Hadoop的主要应用场景: a.数据分析平台. b.推荐系统. c.业务系统的底层存储系统. d.业务监控系统. 2.开发环境:Linux集群(Centos64位)+Window开发模式(win ...
- webpack学习笔记--压缩代码
浏览器从服务器访问网页时获取的 JavaScript.CSS 资源都是文本形式的,文件越大网页加载时间越长. 为了提升网页加速速度和减少网络传输流量,可以对这些资源进行压缩. 压缩的方法除了可以通过 ...
- C# 多线程下 静态类字段异常
- HDU 1074 Doing Homework(DP状态压缩)
题意:有n门功课需要完成,每一门功课都有时间期限以及你完成所需要的时间,如果完成的时间超出时间期限多少单位,就会被减多少学分,问以怎样的功课完成顺序,会使减掉的学分最少,有多个解时,输出功课名字典序最 ...
- Java数据结构与算法结构图