Struts 2 Action Chaining example

apache-struts-logoIn 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>

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>

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>


6 Comments

  • Leo 14 December, 2012, 22:29

    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?

  • shrikant pandey 20 February, 2013, 9:59

    how to call a struts2 action from Ajax auto completer tag

  • wang 2 August, 2013, 16:02

    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 .

  • Pramod 15 September, 2013, 14:59

    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.

  • Eric 18 November, 2013, 14:58
     int a=1;
     <%=a%> 
  • sumit 28 January, 2014, 14:38

    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

Leave a Reply

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

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]