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.