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.
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.