|
Recipe 6.2. Relaying ActionsProblemYou want to link one action directly to another. SolutionUse 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> DiscussionThe 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>
See AlsoThe 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. |
|