簡體   English   中英

使用RedirectAttributes.addFlashAttribute刷新后將對象保留在視圖中

[英]Keep object in the view after refreshing when using RedirectAttributes.addFlashAttribute

感謝Spring 3.1,我可以使用RedirectAttributes.addFlashAttribute使Post / Redirect / Get發生,但是似乎有一個小問題

這是持久化表單對象的方法,然后重定向到視圖以顯示該表單對象:

@RequestMapping(value = "/{formType}/onTheEarch", method = RequestMethod.POST)
public String submitAndRedirect(SomeWebForm someWebForm,
        @PathVariable("formType") String formType,
        final RedirectAttributes redirectAttributes) {

            // do something according to formType
            // .......

            redirectAttributes.addFlashAttribute("webObject", webObject);
            String view = "redirect:/formType/toTheMoon";
    }

這是將用戶定向到顯示表單對象的視圖的方法

@RequestMapping(value = "/{formType}/toTheMoon", method = RequestMethod.GET)
public String submitAndRedirect(@PathVariable("formType") String formType) {
            // do something according to formType
            // .......
            String view = "toTheMoon";
    }

到目前為止,還不錯,但是有一個不足。 當我將視圖刷新到toTheMoon ,一切都消失了。 所以這里的問題是

(1)How does `RedirectAttributes.addFlashAttribute` works? 
(2)How can I keep the object from "FlashAttribute" even after refreshing the page?

我知道第二個問題,我們可以避免RedirectAttributes.addFlashAttribute而只需在URL中傳入任何參數即可實現RGP pattern ,但是,我嘗試避免這種情況,因為參數值是敏感的,不應在瀏覽器上公開給用戶。 那么該怎么辦?

春季文檔第17.6章所述

Flash屬性會在重定向之前(通常在會話中)臨時保存,以便在重定向之后可供請求使用,並立即刪除。

如果您希望結果繼續存在幾個請求,則可以將它們存儲在帶有某些標識符的會話中,並將標識符作為請求參數僅傳遞給結果頁面

我通常做的另一種方法是檢查結果模型是否存在於結果頁面上,如果不只是重定向到錯誤頁面(即:如果用戶確實只是出現錯誤,則用戶無意按刷新/直接訪問結果頁面)。 然后使用javascript進行客戶端刷新預防

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM