2014-07-02 127 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