#java #jsp #struts-1
#java #jsp #struts-1
Вопрос:
Я хочу вызвать метод 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>
классы действий dispactch
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
<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>
Спасибо
Комментарии:
1. Я не вижу ваших отображений
UserAction
в вашемstruts-config.xml
.
Ответ №1:
Просто передайте имя метода в вашу переменную параметра, которую вы хотите вызвать
<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" />