Spring MVC @SessionAttributes和@SessionAttribute用法

本文介绍了Spring MVC中的@SessionAttributes和@SessionAttribute的使用。@SessionAttributes用于在HTTP会话中存储模型属性,适用于控制器级别的会话属性声明,简化了对会话对象的操作和管理。@SessionAttribute则用于获取全局管理的会话属性,例如由过滤器管理的属性,可能在控制器外部存在。
摘要由CSDN通过智能技术生成

Spring MVC @SessionAttributes和@SessionAttribute用法

@SessionAttributes

@SessionAttributes is used to store model attributes in the HTTP Servlet session between requests. It is a type-level annotation that declares the session attributes used by a specific controller. This typically lists the names of model attributes or types of model attributes that should be transparently stored in the session for subsequent requests to access.
通俗的讲,@SessionAttributes透明的将在某个方法中保存的session提升到了类级别,这样对于此Controller中的所有方法都可见。那这又有什么用呢?当然时方便操作了。传统方式上,我们通过HttpSession的setAttribute方法保存了两个session(a和b),当要删除a和b时,一般我们会再次通过HttpSession的removeAttribute进行删除。现在我们可以利用@SessionAttributes将session a 和 b声明为类级别,这样,就可以通过此Controller中的任一方法传入SessionStatus参数,调用其setComplete方法一次将a 和 b清除,这样操作明显简洁了。

代码

@RestController
@SessionAttributes("a", "b") 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值