DispatchAction类(org.apache.struts.actions.DispatchAction) DispatchAction类是Action类的子类,他提供了有实现的execute方法。 我们写的自定义Action类,可以继承DispatchAction类,但不要覆盖execute方法,可以在自定义类中写反回值和参数表都与execute方法相同的方法,可以通过在struts-congfig.xml中为这个action的配置中添加一个参数,来判断调哪一个方法,实际上DispatchAction类就是通过反射机制,通过form中参数调用了自定义Action中的方法,当然这些方法的定义要符合规范,使用继承DispatchAction类的自定义的Action类,也就会共享同一的Action路径。 注意:使用继承DispatchAction类的自定义的Action,只会匹配一个action路径,只能共享一个ActionForm,如果加上校验,会产生form表单的参数不一致的情况,会导致校验无法通过。 例: public class MyAction extends DispatchAction{ ActionForward add(ActionForm form,HttpServletRequest request,HttpServletResponse response ActionMapping mapping) throws Exception { return mapping.findForward("sucess") } } <action path="/add" type="MyAction" parameter="methodName"> <!--parameter属性是和form中隐藏域的名字相对应的--> <forward name="sucess" path="/sucess.jsp"/> </action> <from action="add.do" method="post"> <input type="hidden" name="methodName" value="add"/> <!-- 使用隐藏域为struts传递要调用自定义Action中方法的方法名,是通过与struts-config.xml 中action标签中的parameter和name属性相对应来获取隐藏域的value。 --> <input type="submit" value="submit"/> </from> MappingDispatchAction类(org.apache.struts.actions.MappingDispatchAction) MappingDispatchAction类是DispatchAction的子类,他和DispatchAction不同点就是可以去匹配多个action路径,这样也就是结决了共用ActoinForm的校验问题了,多个Action的路径使用同一的自定义Action类,这样就不用共享同一个ActionForm,也就不会有校验问题了。 例: public class MyAction extends MappingDispatchAction{ ActionForward add(ActionForm form,HttpServletRequest request,HttpServletResponse response ActionMapping mapping) throws Exception { return mapping.findForward("add") } ActionForward del(ActionForm form,HttpServletRequest request,HttpServletResponse response ActionMapping mapping) throws Exception { return mapping.findForward("del") } } <action path="/add" type="MyAction" parameter="add"> <!--parameter属性是指定调用方法的名字--> <forward name="add" path="/add.jsp"/> </action> <action path="/del" type="MyAction" parameter="del"> <forward name="del" path="/del.jsp"/> </action> 在JSP页面中也不用在使用隐藏域传递参数,直接在form中的action中就可以直接使用xxx.do匹配了。 <form action="add.do" method="post"> <input type="submit" value="submit"/> </form> <form action="del.do" method="post"> <input type="submit" value="submit"/> </form> LookupDispatchAction(org.apache.struts.actions.LookupDispatchAction) LookupDispatchAction类也是DispatchAction类的子类,他所实现的功能是解决一个表单多种提交问题的 ,他是通过使用资源文件,用submit按钮的value来作为资源文件中的key所对应的值,通过这个值来找到对用的key,在使用这个key来获得指定Map中所对应的值,这个值就是要调用的方法名。 submit的value---->MessageResource.properties中的key----->Map中key对相应的值---->action 例: <%@taglib uri="http://struts./tags-bean" prefix="bean" %> <form method="post" action="${pageContext.request.contextPath}/lookup/adddel.do"> <input type="submit" value="<bean:message key="button.add" />" name="methodName"> <!--注意name="methodName"是和strut-config.xml中action标签中的parameter属性--> <input type="submit" value="<bean:message key="button.delete" />" name="methodName"> </form> MessageResource.properties button.add=add new user button.delete=delete user 注意:在继承LookupDispatchAction时,要覆盖getKeyMethodMap()方法,并定义Map,向Map中放入指定的键值对。 public class AddDelLookupDispatchAction extends LookupDispatchAction { public Map getKeyMethodMap(){ Map keyMethodMap= new HashMap(); keyMethodMap.put("button.add", "add"); keyMethodMap.put("button.delete", "delete"); return keyMethodMap; } public ActionForward add(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { return mapping.findForward("add"); } public ActionForward delete(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { return mapping.findForward("delete"); } } <action path="/lookup/adddel" type="alan.struts.actions.AddDelLookupDispatchAction" parameter="methodName"> <forward name="add" path="/add.jsp"/> <forward name="delete" path="/delete.jsp" /> </action> <message-resources parameter="alan.struts.message.MessageResource" /> 自定义的Action类的一些规则 1,尽量不要在Action类中使用(静态)成员变量,如果使用要加上同步。 2,尽量使各模块间的耦合性降低,最大限度的针对接口编程。 3,可以将共代码方在覆盖父类的方法中,最后可以用super.xxx(xxx)来调用父类的方法,使用父类的实现,并加上了自定义的功能。 |
|