詳解SpringBoot中@SessionAttributes的使用
簡介
說明
本文介紹SpringBoot中@SessionAttributes的用法。
概述
在默認(rèn)情況下,ModelMap中的屬性作用域是request級別,也就是說,當(dāng)本次請求結(jié)束后,ModelMap 中的屬性將銷毀。如果希望在多個請求中共享ModelMap中的屬性,必須將其屬性轉(zhuǎn)存到session 中,這樣 ModelMap 的屬性才可以被跨請求訪問。
Spring 允許我們有選擇地指定 ModelMap 中的哪些屬性需要轉(zhuǎn)存到 session 中,以便下一個請求屬對應(yīng)的 ModelMap 的屬性列表中還能訪問到這些屬性。這一功能是通過類定義處標(biāo)注 @SessionAttributes 注解來實(shí)現(xiàn)的。
代碼
后端代碼
Controller
@Controller
@RequestMapping("/anno")
@SessionAttributes(value={"msg"}) // 把Mapmodel中名字為msg的屬性存入到session屬性列表中
public class AnnoController {
@RequestMapping(value="/testSessionAttributes")
public String testSessionAttributes(Model model){
System.out.println("testSessionAttributes...");
// 底層會存儲到request域?qū)ο笾?
model.addAttribute("msg","testSessionAttributes");
return "success";
}
@RequestMapping(value="/getSessionAttributes")
public String getSessionAttributes(ModelMap modelMap){
System.out.println("getSessionAttributes...");
String msg = (String) modelMap.get("msg");
System.out.println(msg);
return "success";
}
@RequestMapping(value="/delSessionAttributes")
public String delSessionAttributes(SessionStatus status){
status.setComplete();//刪除session域中的存入的數(shù)據(jù)
return "success";
}
}
前端代碼
success.html
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入門成功</h3>
${ msg }
${sessionScope}
</body>
</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
前端

到此這篇關(guān)于詳解SpringBoot中@SessionAttributes的使用的文章就介紹到這了,更多相關(guān)SpringBoot @SessionAttributes內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot普通類中如何獲取session問題
- SpringBoot3整合MyBatis出現(xiàn)異常:Property?'sqlSessionFactory'or?'sqlSessionTemplate'?are?required
- SpringBoot集成redis與session實(shí)現(xiàn)分布式單點(diǎn)登錄
- SpringBoot Session接口驗(yàn)證實(shí)現(xiàn)流程詳解
- SpringBoot整合SpringSession實(shí)現(xiàn)分布式登錄詳情
- SpringBoot?整合?Spring-Session?實(shí)現(xiàn)分布式會話項目實(shí)戰(zhàn)
- SpringBoot中HttpSessionListener的簡單使用方式
- SpringBoot2.x設(shè)置Session失效時間及失效跳轉(zhuǎn)方式
- SpringBoot下實(shí)現(xiàn)session保持方式
- Spring?Session(分布式Session共享)實(shí)現(xiàn)示例
相關(guān)文章
java?安全?ysoserial?CommonsCollections6?分析
這篇文章主要介紹了java?安全?ysoserial?CommonsCollections6示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
基于Spring AOP實(shí)現(xiàn)日志自動打印功能
這篇文章主要介紹了基于Spring AOP實(shí)現(xiàn)日志自動打印功能,文中通過代碼示例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-01-01
SpringBoot整合MongoDB的實(shí)現(xiàn)代碼
自己本科時候一直使用的是Mysql,目前的課題組使用的是MongoDB,因此就花了一部分時間整理了一下,實(shí)現(xiàn)springboot與MongoDB的整合,并且實(shí)現(xiàn)基本的增刪改查操作,從頭到尾給出一個完整的案例。2021-05-05
Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù)
這篇文章主要介紹了Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
springboot項目mysql-connector-java默認(rèn)版本如何查看
這篇文章主要介紹了springboot項目mysql-connector-java默認(rèn)版本如何查看問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot實(shí)現(xiàn)elasticsearch索引操作的代碼示例
這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 索引操作,文中有詳細(xì)的代碼示例,感興趣的同學(xué)可以參考閱讀下2023-07-07
SpringBoot Starter依賴原理與實(shí)例詳解
SpringBoot中的starter是一種非常重要的機(jī)制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進(jìn)starter,應(yīng)用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要加載的信息并啟動相應(yīng)的默認(rèn)配置。starter讓我們擺脫了各種依賴庫的處理,需要配置各種信息的困擾2022-09-09

