@SessionAttributes 的使用
@SessionAttributes
注解只用作用在 类 上,作用是将指定的 Model 的键值对保存在 session 中。可以让其他请求共用 session 中的键值对。
指定保存的属性名
作用是将 Model 中指定 属性名 的键值对保存在 session 中。
下面的代码中,/user-information
请求将 Model 中的 user 对象添加到了 session 中。所以在 /testSession
请求中,虽然没有在 Model 中设置键值对,但是在页面中仍然可以取得 user 对象。因为 user 对象被添加到了 session 中,其他请求仍然可以取得。
使用 @SessionAttributes(value = {"user1", "user2", "user3"})
可以指定多个添加到 session 中的 Model 属性名。
package com.pudding.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.pudding.bean.User;
@Controller
@SessionAttributes("user")
public class SessionAttributesController {
@RequestMapping("/user-information")
public void setSessionModel(Model model) {
User user = new User("小明", 18);
model.addAttribute("user", user);
}
@RequestMapping("/testSession")
public String get() {
return "test";
}
}
指定保存的属性类型
作用是将 Model 中指定 类型 的键值对保存在 session 中。
同样的,可以使用 @SessionAttributes(types = {User.class, Person.class})
来指定多个类型。
package com.pudding.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.pudding.bean.User;
@Controller
@SessionAttributes("user")
public class SessionAttributesController {
@RequestMapping("/user-information")
public void setSessionModel(Model model) {
User user = new User("小明", 18);
model.addAttribute("user", user);
}
@RequestMapping("/testSession")
public String get() {
return "test";
}
}
混合使用
当然,你也可以混合使用上面的两种方式:@SessionAttributes(value = {"user1", "user2"}, types={Persion.class})
会将 model 中属性名为 user1 和 user2 以及类型为 Person 的属性添加到 session 中。
@SessionAttributes 的使用的更多相关文章
- 在SpringMVC中使用@SessionAttributes和@ModelAttribute将数据存储在session域中
今天在我的springMVC项目--图书管理系统中,希望在登录时将登录的Users存在session中,开始是准备在controller中使用Servlet API中的对象,可是一直无法引用,不知道为 ...
- Spring MVC 对于@ModelAttribute 、@SessionAttributes 的详细处理流程
初学 Spring MVC , 感觉对于 @ModelAttribute 和 @SessionAttributes 是如何被Spring MVC处理的,这一流程不是很清楚, 经过Google资料,有了 ...
- @SessionAttributes与HttpSession
SessionAttributes注解将model中与它同名的属性保存在HttpSession中. 在controller的方法执行完毕后处理SessionAttributes注解并保存的,是Hand ...
- 5、处理模型数据ModelAndView、Map、Model以及@SessionAttributes注解
Spring MVC提供了以下几种途径输出模型数据 —— ModelAndView: 处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据.数据会添加到request域中. ...
- @PathVariable和@RequestParam的区别,@SessionAttributes
简介: handler method参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类: A:处理requet uri部分(这里指uri template中variable,不 ...
- SpringMVC @SessionAttributes注解
@SessionAttributes 注解只能作用到类上 @SessionAttributes(value={"user"},types={String.class}) @Sess ...
- SpringMVC(十一):SpringMVC 处理输出模型数据之SessionAttributes
Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...
- SpringMVC SessionAttributes 简述
使用SpringMVC时,我们会发现网络上有关SessionAttributes注解的内容非常少,更多的人甚至推荐你继续用HttpServletRequest中的session管理方法来控制Sessi ...
- SpringMVC(十六) 处理模型数据之SessionAttributes
@SessionAttributes原理 默认情况下Spring MVC将模型中的数据存储到request域中.当一个请求结束后,数据就失效了.如果要跨页面使用.那么需要使用到session.而@Se ...
- Spring MVC @SessionAttributes注解
@SessionAttributes原理 默认情况下Spring MVC将模型中的数据存储到request域中.当一个请求结束后,数据就失效了.如果要跨页面使用.那么需要使用到session.而@Se ...
随机推荐
- JUC 中提供的限流利器-Semaphore(信号量)
在 JUC 包下,有一个 Semaphore 类,翻译成信号量,Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.Semaphore 跟锁 ...
- [Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example because scrolling is in progress and cannot be interrupted
字面意思大概就是: [干预]忽略尝试取消带有cancelable = false的touchmove事件的尝试,例如,因为滚动正在进行并且无法中断. 解决方法: 1.添加样式更改 将滑动报错的标签样式 ...
- 《Mathematical Analysis of Algorithms》中有关“就地排列”(In Situ Permutation)的算法分析
问题描述 把数列\((x_1,x_2,\cdots,x_n)\)变换顺序为\((x_{p(1)},x_{p(2)},\cdots,x_{p(n)})\),其中\(p\)是\(A=\{1,2,3,\cd ...
- Java 获取 IP
/** * 获取访问者IP. * 在一般情况下使用 Request.getRemoteAddr() 即可,但是经过 nginx 等反向代理软件后,这个方法会失效. */ private String ...
- A股调整结束 很可能明天开始阶段性反弹 目标3100左右
A股调整结束 很可能明天开始阶段性反弹 目标至少AC等距. A浪2685-3059=374 C浪2715+374=3089 长周期看 A股处于 2300-4300长期震荡中.A股的牛市还要等几年.
- Salesforce LWC学习(十五) Async 以及 Picklist 公用方法的实现
本篇参考:salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type) https://developer.salesfo ...
- MATLAB 排序、拟合
一.数据排序整合 1.随机生成的数,从小到大排序 clear rand('seed',1)%设置随机种子,确保随机数一样 edge_range=unifrnd (1, 10, 1, 10) edge_ ...
- 关于k12领域Lucene.net+pangu搜索引擎设计开发的一些回顾
在中国最大的教育资源门户网站两年期间, 黄药师负责学科网搜吧的设计与开发…正好赶上了公司飞速发展的阶段.. 作为专注于k12领域内容与服务的互联网公司的一员,同时整个公司又在积极提升用户体验的氛围中, ...
- Unity引擎入门——制作第一个2D游戏(2)角色移动与动画
在上一节的内容里,我们已经创建出了一个主角,也搭建了一个简单的场景. 传送门:https://www.cnblogs.com/zny0222/p/12653088.html 既然有了主角,要怎样才能让 ...
- O - 推箱子 HDU - 1254(bfs_box + bfs_man)
O - 推箱子 HDU - 1254 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能 ...