Actions for complex transitions


Perfectly reusable Actions are not a reality yet. Suppose that you have a common page accessed from two different pages and what the page shows where the page goes next depends on where you came from. You can never create totally reusable Actions and chain them in this scenario.

Wiring the handlers

If the web application you are designing is entirely of the format ‚“where you came from drives what to do and where to go next ‚½, then consider using a different approach. Split the current request handling and presenting next page into two different handler classes. Write atomic piece of ‚“do ‚½s as Commands for each. In a separate XML, wire them up together as you would like. The Action class serves very little purpose here other than to figure out which handlers are wired together. In fact a single Action for the whole application suffices. All that this Action does is to look up in the XML for commands to be executed in a pipeline. Similarly if your web application provides personalization features, then you have to create atomic handlers and wire them together dynamically.

State aware Forms

Consider a scenario when you can enter a page from N different places and exit in N different ways. Figure 4.4 shows the scenario. There is a common page. It can be accessed from Page1 and Page2. After executing the common action in common page, the user is forwarded to Page3 and Page4 respectively on success. On pressing Cancel, Page1 and Page2 are shown respectively.


Figure 4.4: Complex Page transition example.

An easy way to achieve this is to track where the user came from in session and then accordingly act in the common action. This however makes the common action less reusable. If a lot of your pages behave in this manner you should consider developing a framework to abstract the complexities of the transition. A simple approach is illustrated here. Start with an interface with two methods as shown below:

 public interface StateAware {       public String getPreviousState();       public String getNextState();     } 

The ActionForms involved in the complex transitions implement this interface. Consider that ActionForm1 is associated with Page1. The ActionForm1 implements the StateAware interface. The getPreviousState() returns the forward for Page1 and the getNextState() returns the forward for Page3. The Common Action now becomes really reusable.

 public class CommonAction extends Action {    public ActionForward execute(ActionMapping mapping,               ActionForm form, HttpServletRequest request,               HttpServletResponse response) throws Exception {        StateAware sw = (StateAware) form;        if (isCancelled(request)) {          return mapping.findForward(sw.getpreviousState());        }        //do common action here        //success        return mapping.findForward(sw.getNextState());    } } 

Refer to http://www.livinglogic.de/Struts/ for more about Struts based workflow approach to solve similar problems.

For multi page forms, use multiple Action classes; one Action per page submission. Multi-page forms generally have buttons with same names : Prev, Next etc. This will result in confusion when forwarding to appropriate page if a single Action class is used to handle buttons with the same names in multiple pages.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net