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.
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?
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)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
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
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 .
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.
[code language="java"] int a=1;[/code]
[code language="html"] <%=a%> [/code]
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
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.
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...
Hi ViralPatel,
Thanks for sharing, Its very useful to many developer.
hi viral, I call action with result type stream but I need available a variable in my page result. how I do it? thk