Executing multiple actions one after another

吃可爱长大的小学妹 提交于 2019-12-05 11:16:38

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>

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.

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!