This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Can someone tell me if this approach is "right" or there is a better way, its something which always gives me a headache when I use struts.
Say I have the situation where, I want to use request scope action forms.
JSP Screen 1 user enters some data JSP Screen 2 Application outputs data based on entry on screen1.
Screen 1 will have an action mapped via the
So the Action casts the form passed to Screen1ActionForm(form)
Gets the data out of it screen1ActionForm.getXXXX()
To prepopulate the Screen2ActionForm
It has to
Create an instance of Screen2ActionForm
Populate it via setXXX methods
Put this instance into the correct scope e.g request.setAttribute(Screen2ActionForm)
Return an ActionForward that maps to Screen2 JSP
What worries me about this approach is that not only does the Action need to know the Class of the ActionForm passed to it (Screen1ActionForm) it also has to know the Class of the Form used in the target jsp (Screen2ActionForm)!
Since the form bean and the JSP are inextricably tied to each other, I see no problem at all architecturally with the fact that if you forward to a JSP, you also have to know the name of and populate the form bean that is used by that JSP.
To keep things a little cleaner, though, I generally delegate logic for setting up a page to a class other than the action class so that I can re-use that logic if I have to set up and display that page again from another action form. That way, the action doesn't really have to know about the action form used by the second page. It just makes a call to another method that does know about it.
Another technique used by many Struts developers called "action chaining". Using this technique, you write a setup action and a process action for each page. The process action then decides which page to display next and then returns an ActionForward that points not to the JSP, but to the Setup Action for the page, which in turn points to the JSP.
This approach has other architectural problems, though, so I personally don't use it. Check out this thread for an interesting discussion on this topic. [ December 18, 2005: Message edited by: Merrill Higginson ]