2014-07-02 185 views
0

我在我的應用程序中使用彈簧安全性。RediectAttributes不能與彈簧安全工作

在我的控制器方法之一,我重定向到另一個控制器方法和附加模型屬性,但我沒有在下一個控制器方法中獲得這些modelAttributes。

是否因爲Spring Security Filter的內部重定向而發生這種情況,這可能會導致重定向閃存屬性被破壞?

這是我的代碼:

@RequestMapping("/method1") 
public String firstMethod(Model model,RedirectAttributes redirectAttributes) { 
    ... do something ... 
    redirectAttributes.addFlashAttributes("message","This is a test message"); 
    return "redirect:/method2"; 
} 

@RequestMapping("/method2") 
public String secondMethod(@ModelAttribute("message") String message, Model model) { 
    ... do something ... 

    return "some_view"; 
} 

在這裏,在secondMethod消息來了爲空白( 「」);

+0

嘗試使用'model.addAttribute()'相反 –

+0

model.addAttribute不會生存重定向 – Pankaj

回答

0

使用addAttribute,而不是addFlashAttributes

閱讀差異here

@RequestMapping("/method1") 
public String firstMethod(Model model,RedirectAttributes redirectAttributes) { 
    redirectAttributes.addAttribute("message","This is a test message"); 
    return "redirect:/method2"; 
} 

@RequestMapping("/method2") 
public String secondMethod(@ModelAttribute("message") String message, Model model) { 
    return "some_view"; 
} 
+0

非常感謝!有效 – Pankaj

0

如果你使用重定向,它不會攜帶標題或屬性。但是你可以維護會話,如果它在同一個應用程序中,你可以使用它。使用重定向意味着創建新的請求。

您更好地使用自定義的falsh範圍

+0

你是對的。但這就是爲什麼從Spring 3.1開始,他們添加了RedirectAttributes,它允許redirec請求攜帶模型屬性。這似乎沒有在我的情況下工作,因爲我認爲一些Spring Security過濾器重定向請求,但沒有添加先前請求的Flash屬性。 – Pankaj

+0

是的,你是正確的。使用自定義Flash範圍 – Nadendla

+0

檢查此鏈接,你可以找到你的答案http://stackoverflow.com/questions/5863472/spring-mvc-custom-scope-bean/5883270#5883270 – Nadendla