DispatchAction 的作用
DispatchAction的作用简单地说就是把原来我们写在多个acton里的操作放在同一个 action里处理。
在Struts-config 中 相关的配置内容
    <action-mappings>
                <action path="/welcome" forward="/index.jsp"/>

                <!-- ======================================= DispatchAction Example -->
                <action path="/dispatch"    forward="/dispatch.jsp"/>
                <action path="/dispatch-submit"    
                                type="org.apache.struts.webapp.dispatch.DispatchExampleAction"
                                parameter="dispatchMethod"
                                name="testForm"
                                scope="request">
                        <exception key="dispatch.NoSuchMethodException"
                                             type="java.lang.NoSuchMethodException"
                                             path="/dispatch.jsp"/>
                        <exception key="dispatch.ServletException"
                                             type="javax.servlet.ServletException"
                                             path="/dispatch.jsp"/>
                        <forward name="success" path="/dispatch.jsp"/>
                </action>
.........
formBean 不在讨论范围,所以省略了。
注意:【 parameter="dispatchMethod"】。 在后面的JSP 页面中,表单提交的时候,要对dispatchMethod 这个参数赋值。
 
2.提交表单页面
.......
<html:form action="dispatch-submit" style="display:inline">
                            <input type="hidden" name="dispatchMethod" value="doFoo" />
                            <html:submit><bean:message key="button.foo.label" /></html:submit>
                    </html:form>
                             
                    <html:form action="dispatch-submit" style="display:inline">
                            <input type="hidden" name="dispatchMethod" value="doBar" />
                            <html:submit><bean:message key="button.bar.label" /></html:submit>
                    </html:form>
                             
                    <html:form action="dispatch-submit" style="display:inline">
                            <input type="hidden" name="dispatchMethod" value="doInvalid" />
                            <html:submit><bean:message key="method.invalid.label" /></html:submit>
                    </html:form>
                             
                    <html:form action="dispatch-submit" style="display:inline">
                            <input type="hidden" name="dispatchMethod" value="execute" />
                            <html:submit><bean:message key="method.execute.label" /></html:submit>
                    </html:form>
........
 
在这页面中,有专门的一个隐藏域名称为【dispatcherMethod】,和struts-config.xml文件中目标Action 参数[parameter=dispatcherMethod]相对应。这样Actiton,会根据传入的隐藏域【name=dispatherMethod】的value值,执行相应的方法。
 
1.当提供的dispatcherMethod 的值,在Action中没有对应的方法时(比如,doInvalid),会报
NoSuchMethodException: Action[/dispatch-submit] does not contain specified method (check logs)
2.如果dispatcherMethod的值为 execute/perform时,报Do not use 'execute' or 'perform' with DispatchAction.
 
3.如果提供的不提供dispatcherMethod参数时,报错:
ServletException: DispatchMapping[/dispatch-noparam] does not define a handler property
3.在一些demo中,其中有有execute(),方法,感觉是必须的。但是我把该方法去掉,并没有影响操作。
下面是我删除的代码:
        private ActionDispatcher dispatcher
                                        = new ActionDispatcher(this,
                                                                ActionDispatcher.DISPATCH_FLAVOR);
        public ActionForward execute(ActionMapping mapping,
                                                                 ActionForm form,
                                                                 HttpServletRequest request,
                                                                 HttpServletResponse response)
                throws Exception {
System.out.println("execute() 执行了");
                return dispatcher.execute(mapping, form, request, response);

        }
 
难道在DispatchAction中该方法可有可无吗?