Chaining actions in Struts

By: Apache Foundation Emailed: 1598 times Printed: 2044 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Chaining actions can be done by simply using the proper mapping in your forward entries in the struts-config.xml file. Assume you had the following two classes:


    /* com/AAction.java */
    ...

    public class AAction extends Action
    {
        public ActionForward
                execute(ActionMapping mapping,
                        ActionForm form,
                        HttpServletRequest request,
                        HttpServletResponse response) throws
                                                      Exception
        {
            // Do something

            return mapping.findForward("success");
        }
    }
    

    /* com/BAction.java */
    ...

    public class BAction extends Action
    {
        public ActionForward
                execute(ActionMapping mapping,
                        ActionForm form,
                        HttpServletRequest request,
                        HttpServletResponse response) throws
                                                      Exception
        {
            // Do something else

            return mapping.findForward("success");
        }
    }
    

Then you can chain together these two actions with the Struts configuration as shown in the following excerpt:


    ...
    <action-mappings type="org.apache.struts.action.ActionMapping">
       <action path="/A"
               type="com.AAction"
               validate="false">
          <forward name="success" path="/B.do" />
       </action>
       <action path="/B"
               type="com.BAction"
               scope="session"
               validate="false">
          <forward name="success" path="/result.jsp" />
       </action>
    </action-mappings>
    ...
    

Here we are assuming you are using a suffix-based (.do) servlet mapping, which is recommended since module support requires it. When you send your browser to the web application and name the action A.do (i.e. http://localhost:8080/app/A.do) it will execute AAction.execute(), which will then forward to the "success" mapping.

This causes the execution of BAction.execute() since the <forward> entry for "success" in the configuration file uses the .do suffix.

Of course it is also possible to chain actions programmatically, but the power and ease of being able to "reroute" your web application's structure using the XML configuration file is much easier to maintain.

As a rule, chaining Actions is not recommended. If your business classes are properly factored, you should be able to call whatever methods you need from any Action, without splicing them together into a cybernetic Rube Goldberg device.

If you must chain Actions, be aware of the following: calling the second Action from the first Action has the same effect as calling the second Action from scratch. If both of your Actions change the properties of a formbean, the changes made by the first Action will be lost because Struts calls the reset() method on the formbean when the second Action is called.


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

nice tutorial

View Tutorial          By: Amit at 2010-04-16 04:48:52
2. View Comment

This is helpful for newbies like me :P

View Tutorial          By: Mustafa at 2011-06-18 23:40:27

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Apache Foundation
When is the best time to validate input in Struts
Chaining actions in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
What is Struts? Which Version of Struts to use?
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
Struts 1 vs Struts 2
Handling Duplicate Form Submissions in Struts
ActionErrors and ActionError in Struts
Introduction to Struts Architecture
Handling multiple buttons in HTML Form in Struts
DispatchAction in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
The directories and files of a Struts application
Editing web.xml in a Struts Application
Creating the first application using Struts 2
Model 1 Architecture
MVC Architecture (Model 2 Architecture)
Most Emailed Articles (in Struts)
Editing struts-config.xml in a Struts Application
IncludeAction in Struts
When is the best time to validate input in Struts
What is Struts? Which Version of Struts to use?
Is Struts the most popular web application framework for Java?
What is a Model View Controller (MVC) Model?
History of Struts
WebWork 2 is now Apache Struts 2
Configuring JDBC DataSources in Struts
Struts Classes
Downloading and installing Struts
The directories and files of a Struts application
Editing web.xml in a Struts Application
Creating the first application using Struts 2
Struts 1 vs Struts 2