Struts 2 Action Chaining example

apache-struts-logo

In Struts 2, sometimes you may want to process another action when one action completes. For example on successfully submitting a form you want to render output from other action. This is called Action chaining in Struts 2. One action leads to another one and so on. Request > Action 1 > Action 2 > Response In Struts 2, this can be achieved by Chain Result. The Chain Result is a result type that invokes an Action with its own Interceptor Stack and Result. This Interceptor allows an Action to forward requests to a target Action, while propagating the state of the source Action. Below is an example of how to define this sequence.

<package name="public" extends="struts-default"> <action name="createUserAccount" class="net.viralpatel.CreateAccountAction"> <result name="success" type="chain">login</result> </action> <action name="login" class="net.viralpatel.LoginAction"> <result name="success" type="chain">showDashboard</result> </action> <action name="showDashboard" class="net.viralpatel.DashboardAction"> <result name="success">/WEB-INF/jsp/dashboard.jsp</result> </action> </package>
Code language: HTML, XML (xml)

In above code we define three actions: createUserAccount, login and showDashboard. Notice how each action is chained to the next one using <result type="chain"> tag. Thus when user logs in first time in system, the createAccount action will be used. Once account is created user is forwarded to login action.

Disadvantage of Action Chaining

The Action chaining must be used with precaution. Struts2 documentation too doesn’t support this feature. Experience shows that chaining should be used with care. If chaining is overused, an application can turn into “spaghetti code”. Actions should be treated as a Transaction Script, rather than as methods in a Business Facade. Be sure to ask yourself why you need to chain from one Action to another. Is a navigational issue, or could the logic in Action2 be pushed back to a support class or business facade so that Action1 can call it too?

Alternative: Redirect After Post

You can use Redirect After Post mechanism in cases where you cannot avoid chaining other actions. This is more or less similar to Action chaining with a major difference. The response is told to redirect the browser to the specified location (a new request from the client). The consequence of doing this means that the action (action instance, action errors, field errors, etc) that was just executed is lost and no longer available. This is because actions are built on a single-thread model. The only way to pass data is through the session or with web parameters (url?name=value) which can be OGNL expressions.

<result name="success" type="redirect"> <param name="location">foo.jsp</param> <param name="parse">false</param> </result>
Code language: HTML, XML (xml)

In above result mapping we defined attribute type as redirect. Also notice how we defined two parameters location and parse.

  • location (default) – the location to go to after execution.
  • parse – true by default. If set to false, the location param will not be parsed for Ognl expressions.

The above example of action chaining (create account > login > dashboard) can be implemented using Redirect as follows:

<action name="createUserAccount" class="net.viralpatel.CreateAccountAction"> <result name="success" type="redirect">login</result> </action> <action name="login" class="net.viralpatel.LoginAction"> <result name="success" type="redirect">showDashboard</result> </action> <action name="showDashboard" class="net.viralpatel.DashboardAction"> <result name="success">/WEB-INF/jsp/dashboard.jsp</result> </action>
Code language: HTML, XML (xml)

Get our Articles via Email. Enter your email address.

You may also like...

10 Comments

  1. Leo says:

    This is the code I tried
    login showDashboard /WEB-INF/jsp/dashboard.jsp

    But it shows error message as ,
    Multiple annotations found at this line:
    – Attribute “path” is required and must be specified for element type “action”.
    – The content of element type “action” must match “(icon?,display-
    name?,description?,set-property*,exception*,forward*)”.

    at
    Can anyone resolve this issue?

  2. shrikant pandey says:

    how to call a struts2 action from Ajax auto completer tag

  3. wang says:

    when i use Action Chain,i have set a validation for the first action,but when the form validation is not pass ,i can’t display the fielderrors from the first action on the JSP page used .

  4. Pramod says:

    can we populate an array using paginated display table with textfield in jsp for submission. My application involves struts, java,jsp,mysql. can you mail some sample jsp for such a use case.

  5. Eric says:

    int a=1;


    <%=a%> 

  6. sumit says:

    i want to store value from jsp file with using your example . But in database value store null please give me suggestion how to solve this problem

  7. rachana says:

    Hello Sir,

    I want to get the action name in my interceptor class so that it give the appropriate result.How to get the particular action in interceptor in case of action chaining.

  8. Indrajit says:

    I m developing application using struts2 I have one problem…
    When user login into the system he can view their profile….when click on update data also update into database but not showing on their profile page at the same time..
    How can achieve this kind of feature using session? Help me…

  9. Dinesh Krishnan says:

    Hi ViralPatel,

    Thanks for sharing, Its very useful to many developer.

  10. daniele galassi says:

    hi viral, I call action with result type stream but I need available a variable in my page result. how I do it? thk

Leave a Reply

Your email address will not be published. Required fields are marked *