如何解决Struts:从另一个DispatchAction调用DispatchAction的方法
| 我想从另一个调度动作中调用一个dispatchAction方法。 我希望当我单击更新或删除时,\“内部显示用户方法\”将在我的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>
动作班
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,HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm) form;
userForm.setMessage(\"Inside update user method.\");
return mapping.findForward(SUCCESS);
}
public ActionForward delete(ActionMapping mapping,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,HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm) form;
userForm.setMessage(\"Inside display user method.\");
return mapping.findForward(SUCCESS);
}
}
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\" />
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。