DispatchAtion:
- org.apache.struts.actions.DispatchAction
这个类是个抽象类,但实现父类Action的execute方法,在项目中重写这个类可以专注于执行方法面不必自己实现execute方法
直接给一个例子:
1.继承DispatchAtion类,其包括一些方法,如checkUser()、logon()、logout()等,这些方法有一个共同的模板:
public ActionForward XXXmethod( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ... ... }
package examples; import javax.servlet.http.*; import org.apache.struts.action.*; import org.apache.struts.actions.*; public class AccountAction extends DispatchAction { public ActionForward login( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 進行一些Login的邏輯 return mapping.findForward("success"); } public ActionForward logout( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 進行一些Logout的邏輯 return mapping.findForward("success1"); } }
2.在struts-config.xml如下配置:
<global-forwards> <forward name="logon" path="/logon.do"/> <forward name="error" path="/error.do" redirect="false" contextRelative="true"/> <forward name="main" path="/main.do" redirect="false"/> <forward name="checkOut" path="/checkOut.do" /> <forward name="leftFrame" path="/leftFrame.do" redirect="false"/> </global-forwards> <action-mappings> <action path="/logon" forward="/logon.jsp"/> <action path="/logon-validate" input="/logon.jsp" name="LogonForm" parameter="todo" type="com.amerisia.ebills.studio.action.LogonAction" validate="false"> </action> <action path="/logout" type="com.amerisia.ebills.studio.action.LogoutAction"/> <action path="/online" type="com.amerisia.ebills.studio.action.OnlineAction"/> <action path="/UserOnlineAction" input="/showOnlineUser.jsp" name="UserOnlineForm" parameter="todo" type="com.amerisia.ebills.studio.action.UserOnlineAction"> <forward name="showOnlineUser" path="/showOnlineUser.jsp"/> </action>
DynaActionForm
- org.apache.struts.action.DynaActionForm
写ActionForm时要写上它的setter/getter方法,简单的ActionForm可以用这个来代替,它其实是ActionForm的一个子类,用Map方式存储,配置方法如下:
struts-config.xml中:
<form-beans> <form-bean name="LogonForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> </form-bean> <form-bean name="UserOnlineForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="userId" type="java.lang.String"/> </form-bean> </form-beans> <form-beans>
如此可以取得它:
DynaActionForm dynaActionForm = (DynaActionForm) form; String username = (String) dynaActionForm.get("username"); String password = (String) dynaActionForm.get("password");
这个(DynaActionForm)form是方法传入来的参数,这个方法的参数列表是固定的,是DispatchAction的执行方法。
时间: 2024-10-10 06:30:47