@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. 自执行匿名函数: (function() { /* code */ })();

    1,常见格式:(function() { /* code */ })(); 2,解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括 ...

  2. flutter No material widget found textfield widgets require a material widget ancestor

    Error states that TextField widgets require a Material widget ancestor. Simply wrapping your whole l ...

  3. Netty沾包和拆包

    1.连着发两条,会沾在一起,这就是沾包 2.包尾添加特殊分隔符,接收方通过特殊分隔符切分报文区分,这就是拆包 在ChatServerInit类.ChatClientInit类分别加入以下代码 Byte ...

  4. java概念基础笔记整理

    1.构造方法没有类型,有类型的不是不叫构造方法. 2.一个类的的成员变量可以是java允许的任何数据类型,一个类可以把某个对象作为自己的一个成员变量,如果用这样的类创建对象,那么该对象中就会其他对象, ...

  5. Python属性(@property)

    创建用于计算机的属性 在Python中,可以通过@property(装饰器)将一个方法转换为属性,从而实现用于计算的属性.将方法转换为属性后,可以直接通过方法名来访问方法,而不需要再添加一对小括号&q ...

  6. python http请求类

    # -*- coding: UTF-8 -*- # coding="utf-8" import httplib2 import json from urllib.parse imp ...

  7. 【C++ Primer | 15】访问控制与继承、继承中的类作用域

    1. 只有D继承B的方式是public时,用户代码才能使用派生类向基类的转换:如果D继承B的方式是受保护的或者私有的,则用户代码不能使用该转换. 2. 不论D以什么方式继承B,D的成员函数和友员函数都 ...

  8. Win10任务栏通知区域上已卸载程序无效图标选项如何清除?

    在Win10系统中,大部分用户都已经知道在“选择在任务栏上显示哪些图标”来让一些软年图标显示,一些隐藏,不过使用Win10系统久了之后发现,在设置通知区域图标中有很多已经卸载程序的无效选项!这让设置时 ...

  9. Echarts-各个配置项详细说明总结【转】

    1.图表标题 1 title: { 2 x: 'left', // 水平安放位置,默认为左对齐,可选为: 3 // 'center' ¦ 'left' ¦ 'right' 4 // ¦ {number ...

  10. [转] whistle--全新的跨平台web调试工具

    whistle是基于Node实现的跨平台web调试代理工具,类似的工具有Windows平台上的Fiddler+Willow,基于Java实现的Charles,及公司同事基于Node实现的Livepoo ...