第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Spring 清除會(huì)話范圍 bean

Spring 清除會(huì)話范圍 bean

慕哥9229398 2023-07-28 09:42:32
我創(chuàng)建了一個(gè)會(huì)話范圍的 bean,如下所示:@Bean@Scope(    value = WebApplicationContext.SCOPE_SESSION,    proxyMode = ScopedProxyMode.TARGET_CLASS)public TodoList todos() {    return new TodoList();}我已將 bean 添加為模型屬性,并且可以在多個(gè)頁(yè)面上顯示 bean。@GetMapping("/todos.html")public String list(Model model) {    model.addAttribute("todos", todos);    return "scopedproxytodos";}但我不確定如何清除會(huì)話中的屬性。@PostMapping("/end")public ModelAndView endSession(SessionStatus sessionStatus, Model model) {    model.addAttribute("todos", new TodoList());    sessionStatus.setComplete();    return new ModelAndView("redirect:/");}
查看完整描述

1 回答

?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

我已將 bean 添加為模型屬性,并且可以在多個(gè)頁(yè)面上顯示 bean:


...


`model.addAttribute("todos", todos);`

...


并不真地。這僅在當(dāng)前視圖中添加該屬性。


為了實(shí)現(xiàn)這一點(diǎn),你應(yīng)該TodoList用@SessionScope以下注釋你的類:


@SessionScope

@Component

public class TodoList {

? ? // ...

}

然后注釋需要具有todoswith對(duì)話范圍的控制器@SessionAttributes,并將其注入 with @ModelAttribute。未經(jīng)測(cè)試,但類似的東西:


@Controller

@SessionAttributes("todos")

public class FooController {?


? @GetMapping("/todos.html")

? public String list(@ModelAttribute TodoList todos) {

? ? // set the todos value

? ? return "scopedproxytodos";

? }


}

您可以在任何其他帶有 注釋的控制器中使用它@SessionAttributes("todos")。


要?jiǎng)h除todos當(dāng)前對(duì)話會(huì)話,請(qǐng) status.setComplete()像您一樣使用。

在你的情況下,你可以刪除它:


@PostMapping("/end")

public ModelAndView endSession(SessionStatus sessionStatus, Model model) {

? ? sessionStatus.setComplete();

? ? return new ModelAndView("redirect:/");

}

并讓具有會(huì)話范圍的控制器根據(jù)需要重新初始化它(FooController實(shí)際上是這樣做的)。

另一種方法是重新初始化它而不是刪除它。

例如 :


@PostMapping("/end")

public ModelAndView endSession(@ModelAttribute TodoList todos) {

? ? todos.clear();

? ? return new ModelAndView("redirect:/");

}

該解決方案的優(yōu)點(diǎn):

  • 更簡(jiǎn)潔/更少冗長(zhǎng)

  • 不需要為每個(gè)會(huì)話創(chuàng)建 bean。就開銷而言,Bean 比存儲(chǔ)在會(huì)話中的值更昂貴。


該類SessionStatus管理 spring 會(huì)話的屬性:

可以注入到處理程序方法中的簡(jiǎn)單接口,允許它們發(fā)出會(huì)話處理已完成的信號(hào)。然后,處理程序調(diào)用程序可以進(jìn)行適當(dāng)?shù)那謇?,例如在此處理程序的處理期間隱式創(chuàng)建的會(huì)話屬性

這意味著它清除了存儲(chǔ)在 Spring 處理程序會(huì)話中的屬性(與經(jīng)典的 http 會(huì)話不同),而不是存儲(chǔ)在 http 請(qǐng)求中的屬性,實(shí)際上您沒有在 spring 會(huì)話屬性中添加任何內(nèi)容。
所以那是沒用的。


查看完整回答
反對(duì) 回復(fù) 2023-07-28
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)