Struts 2 Action Chaining example
- By Viral Patel on November 30, 2012
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>
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>
Get our Articles via Email. Enter your email address.
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?
how to call a struts2 action from Ajax auto completer tag