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,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值