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 ...
随机推荐
- SpringMVC文件下载与JSON格式
点击查看上一章 现在JSON这种数据格式是被使用的非常的广泛的,SpringMVC作为目前最受欢迎的框架,它对JSON这种数据格式提供了非常友好的支持,可以说是简单到爆. 在我们SpringMVC中只 ...
- 【kafka】celery与kafka的联用问题
背景:一个小应用,用celery下发任务,任务内容为kafka生产一些数据. 问题:使用confluent_kafka模块时,单独启用kafka可以正常生产消息,但是套上celery后,kafka就无 ...
- jsp 运行时报错Cannot find a method to write property [firstName] of type [java.lang.String] in a bean of type [main.Employee]
原因: 代码没有安装bean的格式写 setFirstName写成了setFristName 错误代码 public void setFristName(String firstName) { thi ...
- java-HTML&javaSkcript&CSS&jQuery&ajax( 八)
一.JavaScript教程笔记 1.在web页面中一般使用JavaScript脚本语言,支持跨平台,跨浏览器,驱动网页,与用户交互.另外Node.js把JavaScript引入到了服务器端. Jav ...
- hdu1198 普通的并查集
今天开始(第三轮)并查集,,之前学的忘了一些 本题很简单直接上代码 #include<iostream> #include<cstring> #include<cstdi ...
- shell 脚本加密
日常编写shell脚本时会写一些账号和密码写入脚本内,但是不希望泄露账号密码,所以对shell脚本进行加密变成可执行文件. 主要使用 shc 对 Linux shell 脚本加密,shc是一个专业的加 ...
- Jmeter测试demo
复制代码,保存为.jmx文件 需要安装插件: JMeterPlugins-ExtrasLibs E:\软件\apache-jmeter-3.0\lib\ext <?xml version=&qu ...
- python selenium打开新窗口,多窗口切换
# coding=utf-8 from selenium import webdriver browser=webdriver.Firefox() browser.maximize_window() ...
- jmeter 中如何一次运行多条sql语句
在jmeter测试mysql中如何一次运行多条sql语句 allowMultiQueries=true 注意:太低版本的mysql和jdbc不支持,最好用最新版的
- python 通用装饰器,带有参数的装饰器,
# 使用装饰器对有返回值的函数进行装饰# def func(functionName): # print('---func-1----') # def func_in(): # print(" ...