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中该方法可有可无吗?



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/240791,如需转载请自行联系原作者
相关文章
|
8月前
|
传感器 API
DIS
DIS
104 2
|
前端开发 Java 开发者
Dispatch 设计| 学习笔记
快速学习 Dispatch 设计。
143 0
Dispatch 设计| 学习笔记
|
监控
Dispatch Source 应用
Dispatch Source 源是一个偏底层的函数集合,使用时CPU负荷非常小,尽量不占资源,开发过程中大多是配合定时器使用。
208 0
|
前端开发 Java 开发者
Dispatch设计|学习笔记
快速学习Dispatch设计
Dispatch设计|学习笔记
|
Java 容器 Spring
DispatcherServlet
路径 org.springframework.web.servlet.DispatcherServlet 继承关系 重点关注doService()方法 该方法重写了父类FrameworkServlet的方法 FrameworkServlet在processRequest()方法中调用了...
838 0
DispatcherServlet请求流程解析-doDispatch(三)
上篇文章我们主要看了DispatcherServlet在提供服务之间做的初始化工作,大部门工作都在WebApplicationContext中完成,然后WebApplicationContext是DispatcherServlet的一个属性。