1

I have an object that I fill in a form in the view "submit".

After that, it post the object "WelcomeMessageFinder" in the view "return".

I call a service with that use this object. If the service fails, I want to redirect to the view "submit" and keep the form filled with the previous values.

My issue is that I don't find how to keep the "WelcomeMessageFinder" object after the redirect. It always create a new empty object.

Here is my code :

@Controller
@SessionAttributes("welcomeMessageFinder")
public class SandBoxController extends PortalWebuiController {
    @ModelAttribute("welcomeMessageFinder")
    public WelcomeMessageFinder welcomeMessageFinder() {
        return new WelcomeMessageFinder();
    }

    @RequestMapping(value = "/submit", method = RequestMethod.GET)
    public String submit(WelcomeMessageFinder welcomeMessageFinder, Model model, SessionStatus sessionStatus, HttpSession httpSession) {
        // On Init : a new WelcomeMessageFinder is created
        // After redirect : I want to keep the filled WelcomeMessageFinder but a new one is created ...
        model.addAttribute("zenithUserSession", zenithUserSession);
        return "submitwelcomemessage";
    }

    @RequestMapping(value = "/return", method = RequestMethod.POST)
    public String retun(
            WelcomeMessageFinder welcomeMessageFinder,
            Model model,
            RedirectAttributes redirectAttributes,
            SessionStatus sessionStatus, HttpSession httpSession) {
        // welcomeMessageFinder contains the parameters I enter in the form.
        redirectAttributes.addFlashAttribute("welcomeMessageFinder", welcomeMessageFinder);
        return "redirect:/submit";
    }
}

What can I do to keep the same WelcomeMessageFinder object before and after the redirect ?

I find this question that says that I can't use SessionAttributes with redirect because it doesn't keep the session. And it says to use RedirectAttributes but the attributes seems to be reinitialized.

EDIT :

I finally found my error. This code works, the problem is with my class WelcomeMessageFinder. To add an object in the flash session, this object need to be Serializable. I forget to implements Serializable in my class.

After adding this, it works fine.

Community
  • 1
  • 1
YLombardi
  • 1,755
  • 5
  • 24
  • 45
  • Possible duplicate of [How to pass model attributes from one Spring MVC controller to another controller?](http://stackoverflow.com/questions/7429649/how-to-pass-model-attributes-from-one-spring-mvc-controller-to-another-controlle) – nobeh Nov 20 '15 at 10:40

2 Answers2

1

I finally found my error. This code works, the problem is with my class WelcomeMessageFinder. To add an object in the flash session, this object need to be Serializable. I forget to implements Serializable in my class.

After adding this, it works fine.

YLombardi
  • 1,755
  • 5
  • 24
  • 45
0

it is because of this piece of code

 "@ModelAttribute("welcomeMessageFinder")
    public WelcomeMessageFinder welcomeMessageFinder() {
        return new WelcomeMessageFinder();
    }"

. it is ALWAYS executed before any requestmapping method is called

Si mo
  • 969
  • 9
  • 24