詳解SpringBoot中@SessionAttributes的使用
簡介
說明
本文介紹SpringBoot中@SessionAttributes的用法。
概述
在默認(rèn)情況下,ModelMap中的屬性作用域是request級(jí)別,也就是說,當(dāng)本次請(qǐng)求結(jié)束后,ModelMap 中的屬性將銷毀。如果希望在多個(gè)請(qǐng)求中共享ModelMap中的屬性,必須將其屬性轉(zhuǎn)存到session 中,這樣 ModelMap 的屬性才可以被跨請(qǐng)求訪問。
Spring 允許我們有選擇地指定 ModelMap 中的哪些屬性需要轉(zhuǎn)存到 session 中,以便下一個(gè)請(qǐng)求屬對(duì)應(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..."); // 底層會(huì)存儲(chǔ)到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)容請(qǐng)搜索腳本之家以前的文章或繼續(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)分布式會(huì)話項(xiàng)目實(shí)戰(zhàn)
- SpringBoot中HttpSessionListener的簡單使用方式
- SpringBoot2.x設(shè)置Session失效時(shí)間及失效跳轉(zhuǎn)方式
- SpringBoot下實(shí)現(xiàn)session保持方式
- Spring?Session(分布式Session共享)實(shí)現(xiàn)示例
相關(guān)文章
swagger2和knife4j的詳細(xì)使用教程(入門級(jí))
最近項(xiàng)目中用到了Swagger2和knife4j作為接口文檔,所以下面這篇文章主要給大家介紹了關(guān)于swagger2和knife4j的詳細(xì)使用教程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09FeignMultipartSupportConfig上傳圖片配置方式
這篇文章主要介紹了FeignMultipartSupportConfig上傳圖片配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java 請(qǐng)求跨域問題解決方法實(shí)例詳解
這篇文章主要介紹了java 請(qǐng)求跨域問題解決方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04SpringBoot項(xiàng)目中集成Apollo的方法步驟
本文主要介紹了SpringBoot項(xiàng)目中集成Apollo的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10Java開發(fā)工具Eclipse使用技巧全局搜索和更替
這篇文章主要介紹了Java開發(fā)工具Eclipse使用技巧全局搜索和更替,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Java泛型在集合使用與自定義及繼承上的體現(xiàn)和通配符的使用
泛型又稱參數(shù)化類型,是Jdk5.0 出現(xiàn)的新特性,解決數(shù)據(jù)類型的安全性問題,在類聲明或?qū)嵗瘯r(shí)只要指定好需要的具體的類型即可。Java泛型可以保證如果程序在編譯時(shí)沒有發(fā)出警告,運(yùn)行時(shí)就不會(huì)產(chǎn)生ClassCastException異常。同時(shí),代碼更加簡潔、健壯2021-09-09Java中print、printf、println的區(qū)別
這篇文章主要介紹了Java中print、printf、println的區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-03-03Spring Boot 數(shù)據(jù)校驗(yàn)@Valid+統(tǒng)一異常處理的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot 數(shù)據(jù)校驗(yàn)@Valid+統(tǒng)一異常處理的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04