这篇文章主要通过示例为大家详细介绍了SpringBoot中@SessionAttributes的使用,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
简介
说明
本文介绍SpringBoot中@SessionAttributes的用法。
概述
在默认情况下,ModelMap中的属性作用域是request级别,也就是说,当本次请求结束后,ModelMap 中的属性将销毁。如果希望在多个请求中共享ModelMap中的属性,必须将其属性转存到session 中,这样 ModelMap 的属性才可以被跨请求访问。
Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。
代码
后端代码
Controller
前端代码
success.html
测试
测试1
访问:http://localhost:8080/anno/testSessionAttributes/
前端
测试2
访问:http://localhost:8080/anno/getSessionAttributes/
后端打印
getSessionAttributes...
testSessionAttributes
测试3
访问:http://localhost:8080/anno/getSessionAttributes/
测试4
再次访问:http://localhost:8080/anno/getSessionAttributes/
后端打印
getSessionAttributes...
null
前端
到此这篇关于详解SpringBoot中@SessionAttributes的使用的文章就介绍到这了,更多相关SpringBoot @SessionAttributes内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!