问题
I am in need of a way of executing multiple struts actions with one request. The goal is to minimize the need of request against the server. So what i need is something like a "MultiAction" which gets a list of actions as its parameters which it should execute and then return a "combined" result of this actions.
For example:
- The client is split up in a lot of modules
- One module needs to get information from the server
- There is a proxy at the client handling this request
- This proxy now goes and say "Hey you other modules, i'm going to make a rquest to the server, you need anything?"
- The other modules can now optionally file a request at the proxy
- Then the actual "combined" request is fired to the server and result is again split up and given to the modules that requested it
So my questions are:
- Is there a standard way in Struts2 of how to do something like this?
- Is there a standard "public" way of calling another action manually and getting its results from the value Stack?
回答1:
This can be achieved by using "redirectAction" in result type. following code is the example for same. you have to configure action tag in struts XML according to your requirement of using nested Actions.
<action name="userHomeAction" class="com.etp.connect.struts.action.UserHomeAction">
<result type="redirectAction" name="SUCCESS_EDIT">
<param name="actionName">getUserEditData</param>
<param name="selectedUser">${selectedUser}</param>
</result>
<result name="error">/jsp/userMgmt/Users_Home.jsp</result>
<result name="login">/jsp/loginMgmt/Login.jsp</result>
</action>
回答2:
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.onlineSolution.CreateAccountAction">
<result name="success" type="chain">login</result>
</action>
<action name="login"
class="net.onlineSolution.LoginAction">
<result name="success" type="chain">showDashboard</result>
</action>
<action name="showDashboard"
class="net.onlineSolution.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 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.
回答3:
Well. It is very much possible by using a simple struts result type.
To process multiple actions in a single action, you can use <result name="success" type="redirectAction"></result>
in your result property. With those multiple actions you can also pass n number of parameters by using <param>
property of struts.xml. Following sample code is for your reference:-
<result name="success" type="redirectAction">
<param name="actionName">[.. you can write another action here ..]</param>
<param name="[.. parameter name ..]">${.. parameter name ..}</param>
</result>
来源:https://stackoverflow.com/questions/15875846/executing-multiple-actions-one-after-another