本文研究的主要是spring學(xué)習(xí)之@SessionAttributes的相關(guān)內(nèi)容,具體如下。
在默認(rèn)情況下,ModelMap 中的屬性作用域是 request 級(jí)別是,也就是說(shuō),當(dāng)本次請(qǐng)求結(jié)束后,ModelMap中的屬性將銷(xiāo)毀。如果希望在多個(gè)請(qǐng)求中共享 ModelMap 中的屬性,必須將其屬性轉(zhuǎn)存到 session 中,這樣ModelMap 的屬性才可以被跨請(qǐng)求訪問(wèn)。
spring 允許我們有選擇地指定 ModelMap 中的哪些屬性需要轉(zhuǎn)存到 session 中,以便下一個(gè)請(qǐng)求屬對(duì)應(yīng)的 ModelMap 的屬性列表中還能訪問(wèn)到這些屬性。這一功能是通過(guò)類(lèi)定義處標(biāo)注 @SessionAttributes
注解來(lái)實(shí)現(xiàn)的。
使模型對(duì)象的特定屬性具有 Session 范圍的作用域
package com.baobaotao.web; … import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.SessionAttributes; @Controller @RequestMapping("/bbtForum.do") @SessionAttributes("currUser") //①將ModelMap中屬性名為currUser的屬性 ,放到Session屬性列表中,以便這個(gè)屬性可以跨請(qǐng)求訪問(wèn) public class BbtForumController { … @RequestMapping(params = "method=listBoardTopic") public String listBoardTopic(@RequestParam("id")int topicId, User user, ModelMap model) { bbtForumService.getBoardTopics(topicId); System.out.println("topicId:" + topicId); System.out.println("user:" + user); model.addAttribute("currUser",user); //②向ModelMap中添加一個(gè)屬性 return "listTopic"; } }
分享題目:spring學(xué)習(xí)之@SessionAttributes實(shí)例解析-創(chuàng)新互聯(lián)
標(biāo)題來(lái)源:http://www.rwnh.cn/article14/jdoge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站改版、網(wǎng)站收錄、外貿(mào)建站、網(wǎng)站設(shè)計(jì)公司、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容