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