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 ...
随机推荐
- Android CTS Test
什么是CTS测试?了解这个问题前,我们先来搜索了解一遍“Google GMS 认证”.GMS全称为GoogleMobile Service,即谷歌移动服务.说白了GMS其实就是一系列谷歌的应用集合.谷 ...
- es6简单介绍
1.ECMAScript 6 简介 2.let和const命令 3.变量的解构赋值 4.字符串的扩展之模板字符串 5.正则的扩展 6.数值的扩展 7.函数的扩展 8.数组的扩展 9.对象的扩展 10. ...
- STL的注意事项
template是一个泛化的:使用template时开始仅仅是声明,具体的例如:k<int> a;叫做实例化显式实例化:类似k<int>a:明确指出哪种类型:隐式实例化:类似k ...
- css+js杂记
css的盒子模型分:ie盒子模型和标准盒子模型 区别: 2.选择器 .id选择器(# myid) .类选择器(.myclassname) .标签选择器(div, h1, p) .相邻选择器(h1 + ...
- ajax----发送异步请求的步骤
1)获取(创建)Ajax对象:获取XMLHttpRequest对象2)创建请求:调用xhr的open方法3)在发送请求之前需要设置回调函数:绑定指定xhr的onreadystatechange事件4) ...
- Java+selenium之WebDriver常见特殊情况如iframe/弹窗处理(四)
1. iframe 的处理 查找元素必须在对应的 ifarme 中查找,否则是找不到的 // 传入参数为 frame 的序号,从0开始 driver.switchTo().frame(Int inde ...
- jQuery常见案例
jQuery常见案例 通过jQuery实现全选,反选取消: 选择 地址 端口 1.1.1.1 80 1.1.1.1 80 1.1.1.1 80 1.1.1.1 80 代码实现 <body> ...
- CMD批处理——forfiles命令使用,自动删除过期备份文件
公司服务器用来备份数据的硬盘过段时间就会被备份文件占满,弄得我老是要登录到服务器去手工删除那些老的文件,有时忘记了就会导致硬盘空间不足而无法备份.因为只要保留最近几天的备份,如果可以做一个批处理让系统 ...
- RabbitMq相关运维
# 命令查询所有用户列表rabbitmqctl list_users # 使用命令对 xiandian-admin 用户进行授权set_permissions xiandian-admin '.*' ...
- [转] Shell编程之数组使用
#!/bin/bash #基本数组操作a=(1 2 3) ##()表示空数组echo "第0个元素:"${a[0]}echo "所有元素: "${a[@]}ec ...