Recipe6.2.Relaying Actions


Recipe 6.2. Relaying Actions

Problem

You want to link one action directly to another.

Solution

Use a local or global forward that specifies its destination URL as another action:

<action    path="/FirstAction"            type="com.foo.FirstAction">       <forward name="success" path="/SecondAction.do"/> </action> <action    path="/SecondAction"            type="com.foo.SecondAction">       <forward name="success" path="/second_page.jsp"/> </action>

Discussion

The actions configured in your struts-config.xml are commonly used as the target action of forms on a JSP page. These actions subsequently forward requests to JSP pages for rendering of the view. However, nothing prohibits an action from forwarding the request to another action. This technique is referred to as action relaying.

As you can see from the Solution, setting up a relay from one action to another is easy. The motivation for doing so, however, isn't as apparent, particularly if you have just started developing your Struts application. By relaying actions, you can hook together application features that you had not originally intended.

Consider a point-of-sale web application for a retail business. Salespersons use the application to make sales and to track and maintain data about customers and their purchases. The application supports two main workflows: one for making purchases and one for updating customer data. The developer on this project has created an action with the path /UpdateCustomer.do that allows retrieval and editing of customer information. After an initial release of the application, the developer is given a new requirement: Every time a customer makes a purchase, the salesperson should make updates to the customer data. Though the developer hadn't planned for this workflow, the steps in the flow can be easily linked using the technique shown in the Solution. The action that completes the purchase has the path /SavePurchase.do. The desired workflow can be set up as follows:

<action    path="/SavePurchase"            type="com.foo.SavePurchaseAction">       <forward name="success" path="/UpdateCustomer.do"/> </action>

Avoid Chaining Actions

While relaying can be a convenient mechanism, a pitfall is related to this technique. Once you have developed a number of actions, you may find that you can reuse application functionality by relaying many of these actions together. This practice of setting up relays to relays is known as action chaining. This approach can be attractive if you have coded your business logic into your Action classes instead of delegating logic to a service layer. Essentially, you have created your business API with Struts actions instead of with Java classes and methods. Chaining the actions together in a sequence, as you would call methods of an API, may appear to be a valid approach.

Beware this temptation! If you follow this path, you've essentially substituted Java method calls with HTTP requests. It doesn't take a rocket scientist to know that an HTTP request is much slower than a Java method call. If this chainingthat is, relay to relay to relayseems useful to you, consider refactoring logic in your Action classes into a service layer interface. This gives you the reuse without sacrificing performance or maintainability.


See Also

The Struts User's mailing list has some good discussions on the implications of hooking actions together. Ted Husted discusses this in more detail in a thread archived at http://www.mail-archive.com/struts-user@jakarta.apache.org/msg96565.html.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

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