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

Java Struts:从另一个DispatchAction调用DispatchAction方法,java,jsp,struts-1,Java,Jsp,Struts 1,我想从另一个分派操作调用分派操作的方法。 我希望当我点击更新或删除时,“内部显示用户方法”将显示在我的jsp上 <action path="/dispatch.do?getMethodtocall=display" parameter="getMethodtocall" name="UserForm" scope="session" type="com.tk20.TestDispatchAction"> <forward name="success" path="/in

我想从另一个分派操作调用分派操作的方法。 我希望当我点击更新或删除时,“内部显示用户方法”将显示在我的jsp上

<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" />
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>
<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" />

显示动作类

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);
    }
}
<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" />
public类UserAction扩展了DispatchAction{
/*转发名称=“成功”路径=“”*/
私有最终静态字符串SUCCESS=“SUCCESS”;
/**
*这是在上调用的Struts操作方法
* http://.../actionPath?method=myAction1,
*其中“方法”是元素中指定的值:
* (  )
*/
公共ActionForward添加(ActionMapping映射、ActionForm表单、,
HttpServletRequest请求,HttpServletResponse响应)
抛出异常{
UserForm UserForm=(UserForm)form;
返回映射。findForward(“分派”);
}
公共ActionForward更新(ActionMapping映射、ActionForm表单、,
HttpServletRequest请求,HttpServletResponse响应)
抛出异常{
UserForm UserForm=(UserForm)form;
setMessage(“内部更新用户方法”);
返回映射。findForward(成功);
}
公共ActionForward删除(ActionMapping映射、ActionForm表单、,
HttpServletRequest请求,HttpServletResponse响应)
抛出异常{
UserForm UserForm=(UserForm)form;
setMessage(“内部删除用户方法”);
返回映射。findForward(成功);
}
}

<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" />
公共类TestDispatchAction扩展了DispatchAction{
/*转发名称=“成功”路径=“”*/
私有最终静态字符串SUCCESS=“SUCCESS”;
/**
*这是在上调用的Struts操作方法
* http://.../actionPath?method=myAction1,
*其中“方法”是元素中指定的值:
* (  )
*/
公共ActionForward显示(ActionMapping映射、ActionForm表单、,
HttpServletRequest请求,HttpServletResponse响应)
抛出异常{
UserForm UserForm=(UserForm)form;
setMessage(“内部显示用户方法”);
返回映射。findForward(成功);
}
}
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>
<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" />

函数submitForm(){
document.forms[0].action=“UserAction.do?methodtocall=add”;
document.forms[0]。提交();
}
删除

谢谢

只需将方法名传递给要调用的参数变量即可

<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 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" />


我在
struts config.xml
中没有看到您的
UserAction
映射。
<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" />