2

I am using spring security in my application.

In one of my controller method I am redirecting to another controller method and attaching model Attributes but I am not getting those modelAttributes in next controller method.

Is it happening because of some internal redirection from Spring Security Filter which might be killing those redirect flash attributes?

This is my code :

@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";
}

Here in secondMethod message is coming as blank( "" );

Pankaj
  • 97
  • 2
  • 9

2 Answers2

1

Use addAttribute instead of addFlashAttributes

Read the difference 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";
}
Community
  • 1
  • 1
Nikhil Talreja
  • 2,754
  • 1
  • 14
  • 20
0

If you use redirect it wont carry headers or attribites.But You can maintain the session and you can use it if it's in same application.Using redirect means creating new request.

You better use Custom falsh scope

Nadendla
  • 712
  • 2
  • 7
  • 17
  • You're right. But that's why from Spring 3.1 onwards they have added RedirectAttributes which allows redirec requests to carry model Attributes. This doesn't seem to be working in my case because I think some of Spring Security filter is redirecting request but without adding flash Attributes from previous request. – Pankaj Jul 02 '14 at 05:29
  • Check this link you can find your answer http://stackoverflow.com/questions/5863472/spring-mvc-custom-scope-bean/5883270#5883270 – Nadendla Jul 02 '14 at 05:33