简体   繁体   English

Struts:从另一个 DispatchAction 调用一个 DispatchAction 方法

[英]Struts : call a a method of DispatchAction from another DispatchAction

I want to call aa method of dispatchAction from another dispatch action.我想从另一个调度操作中调用一个 dispatchAction 方法。 I want that when I click on update or delete "Inside display user method" will be diplayed on my jsp.我希望当我点击更新或删除时“内部显示用户方法”将显示在我的 jsp 上。

Struts config file Struts 配置文件

<action-mappings>
    <action input="/index.jsp" parameter="methodtocall" name="UserForm" path="/UserAction" scope="session" type="com.tk20.UserAction">
        <forward name="success" path="/dispatch.do?getMethodtocall=display.do" />
    </action>

    <action path="/dispatch.do?getMethodtocall=display" parameter="getMethodtocall" name="UserForm" scope="session" type="com.tk20.TestDispatchAction">
        <forward name="success" path="/index.jsp" />
    </action>

    <action path="/Welcome" forward="/welcomeStruts.jsp" />
</action-mappings>

dispactch action classes调度动作类

public class UserAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserForm userForm = (UserForm) form;

        return mapping.findForward("dispatch");
    }


    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserForm userForm = (UserForm) form;
        userForm.setMessage("Inside update user method.");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserForm userForm = (UserForm) form;
        userForm.setMessage("Inside delete user method.");
        return mapping.findForward(SUCCESS);
    }
}

public class TestDispatchAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward display(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserForm userForm = (UserForm) form;
        userForm.setMessage("Inside display user method.");
        return mapping.findForward(SUCCESS);
    }

}

JSP page JSP 页

<html>
<head>
<script type="text/javascript">
    function submitForm() {
        document.forms[0].action = "UserAction.do?methodtocall=add";
        document.forms[0].submit();
    }
</script>
</head>
<body>
<html:form action="UserAction">
    <table>
        <tr><td><bean:write name="UserForm" property="message" /></td></tr>
        <tr><td><html:submit value="Add" onclick="submitForm()" /></td></tr>
        <tr><td><html:submit property="methodtocall" value="update" /></td></tr>
        <tr><td><html:submit property="methodtocall">delete</html:submit></td></tr>
    </table>
</html:form>
</body>
</html>

thanks谢谢

Simply pass method name to your parameter variable, which you want to call只需将方法名称传递给您要调用的参数变量

<action path="/dispatch.do?getMethodtocall=display" parameter="getMethodtocall" name="UserForm" scope="session" type="com.tk20.TestDispatchAction">
    <forward name="success" path="/index.jsp" />
</action>

<action path="/Welcome" forward="/welcomeStruts.jsp" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 1.2.9版中的Struts DispatchAction - Struts DispatchAction in version 1.2.9 在Struts 1.2框架中的DispatchAction中进行验证 - Validation in DispatchAction in struts 1.2 framework struts2中DispatchAction类的替代方法 - Alternative of DispatchAction class in struts2 我们可以编写带有void返回类型的struts dispatchAction方法吗? - Can we write struts dispatchAction method with void return type? 如何修复 Struts DispatchAction 上的 StackOverflowError? - How do I fix a StackOverflowError on Struts DispatchAction? DispatchAction ServletException:请求不包含名为“ method”的处理程序参数 - DispatchAction ServletException: Request does not contain handler parameter named 'method' 如何在DispatchAction类的execute方法中获取响应文本 - how to get response text in execute method of DispatchAction class Mockito Junit: ExceptionInIntialize and NullPointer Error on struts DispatchAction.java when running the Junit tests on a class that extends it - Mockito Junit : ExceptionInIntialize and NullPointer Error on struts DispatchAction.java when running the Junit tests on a class that extends it Websphere Portlet迁移,DispatchAction中缺少参数 - Websphere Portlet migration, missing parameter in DispatchAction Struts 2-动态设置方法名称,从一个Action调用另一个Action方法,以执行 - Struts 2 - Dynamically set the method name, from one Action to call another action method, to execute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM