EventDispatchAction作用:
用於處理多個提交動作的EventDispatchAction類。這個類也是DispatchAction的子類,它在使用上要比LookupDispatchAction類容易的多。EventDispatchAction類的基本原理是通過<action>元素的parameter屬性指定多個動作,中間用逗號(,)分隔。每個動作實際上就是<html:submit>標籤的property屬性值。這樣EventDispatchAction類就可以根據每個<html:submit>標籤的屬性值來確定使用者按的是哪個提交按鈕了。
 
【第1步】實現EventDispatchAction的子類
/*
* $Id: EventDispatchActionExample.java 471754 2006-11-06 14:55:09Z husted $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.    See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.    The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* “License”); you may not use this file except in compliance
* with the License.    You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.    See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.struts.webapp.dispatch;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.EventDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
* Example EventDispatchAction.
*
* @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
*/

public class EventDispatchActionExample extends EventDispatchAction {

        private int fooCount;
        private int barCount;

        /**
         * Example “foo” method.
         *
         * @param mapping The ActionMapping used to select this instance
         * @param form The optional ActionForm bean for this request
         * @param request The servlet request we are processing
         * @param response The servlet response we are creating
         *
         * @exception Exception if business logic throws an exception
         */

        public ActionForward doFoo(ActionMapping mapping,
                                                             ActionForm form,
                                                             HttpServletRequest request,
                                                             HttpServletResponse response)
                throws Exception {

                fooCount++;

                ActionMessages messages = new ActionMessages();
                messages.add(“foo”, new ActionMessage(“count.foo.message”, fooCount+””));
                saveMessages(request, messages);

                return (mapping.findForward(“success”));

        }

        /**
         * Example “bar” method.
         *
         * @param mapping The ActionMapping used to select this instance
         * @param form The optional ActionForm bean for this request
         * @param request The servlet request we are processing
         * @param response The servlet response we are creating
         *
         * @exception Exception if business logic throws an exception
         */

        public ActionForward doBar(ActionMapping mapping,
                                                             ActionForm form,
                                                             HttpServletRequest request,
                                                             HttpServletResponse response)
                throws Exception {
                barCount++;

                ActionMessages messages = new ActionMessages();
                messages.add(“bar”, new ActionMessage(“count.bar.message”, barCount+””));
                saveMessages(request, messages);

                return (mapping.findForward(“success”));

        }

}

 
有兩個方法:doFoo和doBar,分別用來處理property屬性值為“doFoo”和“doBar”的<html:submit>標籤提交的請求動作
 
  【第2步】配置EventDispatchAction類
<action path=“/eventAction-default    
                                type=“org.apache.struts.webapp.dispatch.EventDispatchActionExample”
                                parameter=“doFoo,bar=doBar,default=doBar”
                                name=“testForm”
                                scope=“request”>
                        <forward name=“success” path=“/eventAction.jsp”/>
                </action>
 
parameter=”doFoo,bar=doBar,default=doBar”
如果submit標籤不提供property 或者提供了property值,在Action中找不到相關的方法對應時,會提交的default標記的doBar方法中
【第3步】實現有多個提交按鈕的JSP頁面
                 <html:form action=“eventAction-default style=“display:inline”>
                            <html:submit property=“doFoo”><bean:message key=“button.foo.label” /></html:submit>
                    </html:form>
                             
                    <html:form action=“eventAction-default style=“display:inline”>
                     <input type=“hidden” name=“Method” value=“doBar” />
                            <html:submit><bean:message key=“button.bar.label” /></html:submit>
                    </html:form>
                             
                    <html:form action=“eventAction-default style=“display:inline”>
                            <html:submit><bean:message key=“button.default.label” /></html:submit>
                    </html:form>
 
 
執行結果:
 <html:submit><bean:message key=”button.default.label” /></html:submit>,會執行default 指向的方法。如果沒有default,會報
ServletException: Request[/eventAction-submit] does not contain handler parameter.
2.如果在配置檔案中有相關的方法宣告比如parameter=”missingMethod”,但是在Action java檔案中卻沒有missingMethod()方法的實現,會報
NoSuchMethodException: Action[/eventAction-error] does not contain specified method (check logs)
3. 也不能在配置檔案總使用方法名execcute 。否則:
ServletException: Do not use `execute` or `perform` with DispatchAction.
4.如果忘記了在配置檔案中,為Action設定parameter屬性,會報:
ServletException: DispatchMapping[/eventAction-noparam] does not define a handler property