---
title: "（2）DiapacthAction"
author: "bolan392"
date: "2009-07-08T14:25:01Z"
tags: []
slug: "2-diapacthaction"
draft: false
lastmod: "2009-07-08T14:25:01Z"
id: 421874
url: "https://bolan392.iteye.com/blog/421874"
---

**DispatchAction**用于分发的Action,主要的好处是把一些功能类似的Action放到一个Action中，通过传入的不同参数来觉得执行哪个操作.

1. **创建一个类继承DispacthAction,该类中包含多个除方法名称以外其他都和execute()方法一样的多个方法**

          public class OperateAction extends DispatchAction {  
               public ActionForward **add**(ActionMapping mapping, ActionForm form,  
               HttpServletRequest request, HttpServletResponse response)  
               throws Exception {  
               System.out.println("Action.add()");  
               Cal c = new Cal();  
               CalForm f =(CalForm)form;  
               double r = 0.0;  
               ActionMessages errors = new ActionMessages();  
               try{  
                     r = c.add(Double.parseDouble(f.getFirst()),Double.parseDouble(f.getSecond()));  
               }catch(Exception e){  
                     errors.add("num",new ActionMessage("error"));  
                     this.saveErrors(request, errors);  
                     return mapping.findForward("input");  
              }  
              f.setResult(r);  
              return mapping.findForward("result");  
        }  
 public ActionForward **sub**(ActionMapping mapping, ActionForm form,  
   HttpServletRequest request, HttpServletResponse response)  
   throws Exception {  
  System.out.println("Action.sub()");  
  return mapping.findForward("result");  
 }  
 public ActionForward **mul**(ActionMapping mapping, ActionForm form,  
   HttpServletRequest request, HttpServletResponse response)  
   throws Exception {  
  System.out.println("Action.mul()");  
  return mapping.findForward("result");  
 }  
 public ActionForward **div**(ActionMapping mapping, ActionForm form,  
   HttpServletRequest request, HttpServletResponse response)  
   throws Exception {  
  System.out.println("Action.div()");  
  return mapping.findForward("result");  
 }  
}

**2. 在struts-config.xml文件中进行配置，为该Action配置parameter属性**

   &lt;action-mappings &gt;  
    &lt;action  
      name="CalForm"  
      scope="request"  
      path="/add"  
      type="com.rhcj.struts.CalAction"&gt;  
      &lt;forward name="result" path="/result.jsp" /&gt;  
    &lt;/action&gt;  
    &lt;action path="/operate"  
      type="com.rhcj.struts.OperateAction"  
      name="CalForm"  
      scope="request"  
      **parameter="operate"**&gt;  
      &lt;forward name="result" path="/result.jsp" /&gt;  
      &lt;forward name="input" path="/index.jsp"&gt;&lt;/forward&gt;  
    &lt;/action&gt;  
   &lt;/action-mappings&gt;

**3.    创建页面，页面请求中要有一个参数与parameter属性值一致，如：/operate?operate=add，此处表单中使用隐藏域传值**

   **&lt;input type="hidden" name="operate" value=""&gt;**  
    &lt;input type="button" value="+" name="button1"  
     onclick="doSubmit('add')"&gt;  
    &lt;input type="button" value="-" name="button1"  
     onclick="doSubmit('sub')"&gt;  
    &lt;input type="button" value="\*" name="button1"  
     onclick=doSubmit('mul');&gt;  
    &lt;input type="button" value="/" name="button1"  
     onclick="doSubmit('div')"&gt;  
    &lt;script type="text/javascript"&gt;  
     function doSubmit(oper){  
      document.forms\[0].elements\["operate"].value=oper;  
       document.forms\[0].submit();  
     }  
    &lt;/script&gt;

  

  

注意：该页面中隐藏域的name属性&lt;input type=*"hidden"* name=*"operate"*&gt;对应于struts-config.xml中DispacthAction配置的parameter属性值

&lt;action path=*"/operate"*

        type=*"com.rhcj.struts.OperateAction"*

        name=*"CalForm"*

        scope=*"request"*

        parameter=*"operate"*&gt;

      &lt;forward name=*"result"* path=*"/result.jsp"* /&gt;

&lt;/action&gt;

隐藏域的value值要和DispacthAction中的方法名称对应