PDA

View Full Version : Redirect from one SimpleFormController to another


meghus
Mar 10th, 2008, 11:52 PM
HI All,

I am really stuck with this problem where after processing the request using SimpleFormController1, I then need to call a second SimpleFormController2. I have read lots about this is various sites however, I am unable to find out exactly how to do this.

Just to give some background, I need to first save an event then after the save instead of just returning to the save Event screen I have to return to a view which shows all events from the past. This is handled separately in 2 different SimpleFormControllers, so I first need to call the SaveEventController and then secondly the ShowAllEventsController.

The problem is that the ShowAllEventsController takes a command object which I need to pass when I do a redirect. I am trying to approach it as below:

String viewName = "redirect:showAllEvents.htm";
return new ModelAndView(viewName);

This works and does take me to a view put there is no data there since I am not sure how to pass the command object which the second controller expects.

Any help will be much appreciated!

Thanks.

joshk
Mar 11th, 2008, 02:31 AM
Hi meghus,

You are on the right track, but instead of passing the command object, how about just passing a variable or id?

eg.


String viewName = "redirect:showAllEvents.htm";

ModelAndView mav = new ModelAndView(viewName);

mav.addObject("eventId", eventId);

return mav;


Then in the ShowAllEventsController you can implement the formBackingObject method to grab the object out of the database ready for use by showForm and processSubmit methods.

Hope this helps

Josh

meghus
Mar 11th, 2008, 11:31 AM
HI Josh,

Your reponse was helpful, I am now able to pass an id and retrieve it using

request.getParameter("id");

in my secondController. I then implemented the method formBackingObject() to try to set the command object before it enters the workflow. This has affected the normal operation of this controller - I mean that, it now does not return the default command which it used to before entering the onSubmit() method.

I only want to call the formBackingObject() method everytime a new Event is added if not I want it to continue as normal, however I am not able to do this.

Any other ideas about how to bind the model object from Controller1 to my command object in Controller2 will be great. I am still stuck :(

Regards.

joshk
Mar 11th, 2008, 05:50 PM
Hi meghus,

in my secondController. I then implemented the method formBackingObject() to try to set the command object before it enters the workflow. This has affected the normal operation of this controller - I mean that, it now does not return the default command which it used to before entering the onSubmit() method.

There is nothing stopping you from returning the default form backing object if a new event has not been added, you have access to the request and thus can pass any number of parameters to the formBackingObject method as you like, which can help you determine if you should return the default fbo, or return one from the database.

If you like, post your code before the change, and after, and I can show you the updates you can make.

Josh

meghus
Mar 14th, 2008, 11:49 AM
Hi Josh,

I resolved my problem. :)
I did the redirect and passed in the model as below:

String viewName = "redirect:/caseSummary.htm";
ModelAndView mv = new ModelAndView(viewName);
mv.addObject("commandAction", "Case Summary");
mv.addObject("workingCase.id", caseEventCommand.getId() );

My command object had these 2 instance vairbles, 1 of them was a nested instance variable - workingCase.id and commandAction.
Spring automatically did the bindign once the model was named exactly like it is in the command object - even the nested one was bound I had to use the "." for it like workingCase.id.

Thanks soo much for your help.