Struts1中通过DispatchAction实现数的运算

wKioL1OJnRzC-fUHAAC7UfweYpc339.jpg

添加支持(即jar包)后的步骤为:

(1)创建ActionForm

YunsuanForm.java

package com.mstf.struts.form;

import org.apache.struts.action.ActionForm;

public class YunsuanForm extends ActionForm {

          int num1;

          int num2;

 public int getNum1() {

  return num1;

 }

 public void setNum1(int num1) {

  this.num1 = num1;

 }

 public int getNum2() {

  return num2;

 }

 public void setNum2(int num2) {

  this.num2 = num2;

 }  

}

(2)创建控制器

YunsuanAction.java

package com.mstf.struts;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

import com.mstf.struts.form.YunsuanForm;

public class YunsuanAction extends DispatchAction {

    public ActionForward dojia(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){

     YunsuanForm ysf=(YunsuanForm)form;

     int num1=ysf.getNum1();

     int num2=ysf.getNum2();

     int result=num1+num2;

     request.getSession().setAttribute("result", result);

     return mapping.findForward("result");

    }

    public ActionForward dojian(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){

     YunsuanForm ysf=(YunsuanForm)form;

     int num1=ysf.getNum1();

     int num2=ysf.getNum2();

     int result=num1-num2;

     request.getSession().setAttribute("result", result);

     return mapping.findForward("result");

    }

    public ActionForward docheng(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){

     YunsuanForm ysf=(YunsuanForm)form;

     int num1=ysf.getNum1();

     int num2=ysf.getNum2();

     int result=num1*num2;

     request.getSession().setAttribute("result", result);

     return mapping.findForward("result");

    }

    public ActionForward dochu(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){

     YunsuanForm ysf=(YunsuanForm)form;

     int num1=ysf.getNum1();

     int num2=ysf.getNum2();

     int result=num1/num2;

     request.getSession().setAttribute("result", result);

     return mapping.findForward("result");

    }

}

(3)创建视图

index.jsp

<body> 

    <form action="yunsuan.do" method="post">

    <input type="hidden" name="method"/>

      <table border="1">

             <tr>

               <th colspan="2" align="center">数的运算</th>

             </tr>

             <tr>

               <th>第一个数:</th>

               <td><input type="text" name="num1"/></td>

             </tr>

             <tr>

               <th>第二个数:</th>

               <td><input type="text" name="num2"/></td>

             </tr>

             <tr>

               <td colspan="2">

               <input type="submit" value="+" onclick="method.value='dojia'"/>

               <input type="submit" value="-" onclick="method.value='dojian'"/>

               <input type="submit" value="*" onclick="method.value='docheng'"/>

               <input type="submit" value="/" onclick="method.value='dochu'"/>

               </td>

             </tr>

      </table>

    </form>

</body>

result.jsp

<body> 

  <b>

     第一个数为:${YunsuanForm.num1}<br/>

     第二个数为:${YunsuanForm.num2}<br/>

     结果为:${result}<br/>

  </b>

</body>

(4)配置Struts1应用程序

<struts-config>

  <data-sources />

  <form-beans>

    <form-bean name="YunsuanForm" type="com.mstf.struts.form.YunsuanForm">

    </form-bean>

  </form-beans>

  <global-exceptions />

  <global-forwards />

  <action-mappings >

     <action name="YunsuanForm" path="/yunsuan" scope="session" type="com.mstf.struts.YunsuanAction" parameter="method">

     <forward name="result" path="/result.jsp"></forward>

     </action>

  </action-mappings>

  <message-resources parameter="com.mstf.struts.ApplicationResources" />

</struts-config>

(5)发布、运行示例

wKiom1OJnWzxxAO4AACmEtaLjXg116.jpg

wKioL1OJnUCCBUzTAACMjSYAB1Y157.jpg

wKiom1OJnW2BvWVSAACL1iYvRRY941.jpg

wKioL1OJnUDibMA_AACNEtbxtcM851.jpg

wKiom1OJnW3DaSmrAACNZkSesHk791.jpg

总结:DispatchAction是Action的子类,它能够在同一Action类中完成一组相关的业务操作。创建Dispatch类时,无须覆盖execute()方法,而是创建实现实际业务操作的方法,这些方法都应该和execute()方法具有相同的方法签名,即它们的参数和返回类型都对应相同。

DispatchAction类的配置与普通的Action类相似,需要在配置时指定parameter属性。


你可能感兴趣的:(Stuts1)