Stuts2 使用DispatchAction做一个简单的计算器

  使用DispatchAction做一个简单的计算器

 

 DispatchAction是Struts中Action中的一种,为一种动态Action,可以处理相同类型的方法

 

 1.calculator.jsp(计算页面)

 

 

view plain copy to clipboard print ?
  1. <%@ page language="java" pageEncoding="GB18030"%>  
  2.   
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.     <title>calculator.jsp</title>  
  7.     <mce:script type="text/javascript"><!--  
  8.        function fun(obj)  
  9.        {  
  10.           var element=document.getElementById("method");  
  11.           element.value=obj;  
  12.           alert(element.value);  
  13.           form1.submit();  
  14.        }  
  15.       
  16. // --></mce:script>   
  17.   </head>  
  18.     
  19.   <body>  
  20.     <center>  
  21.       <form action="calculator.do" method="get" id="form1">  
  22.           <input type="text" name="num1"/><br>  
  23.           <input type="text" name="num2"/><br>  
  24.           <input type="hidden" id="method" name="method">  
  25.           <input type="button" name="add" value="加" onclick="fun('add')">  
  26.           <input type="button" name="sub" value="减" onclick="fun('sub')">  
  27.           <input type="button" name="mul" value="乘" onclick="fun('mul')">  
  28.           <input type="button" name="div" value="除" onclick="fun('div')">  
  29.       </form>  
  30.     </center>  
  31.   </body>  
  32. </html>  
<%@ page language="java" pageEncoding="GB18030"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>calculator.jsp</title> <mce:script type="text/javascript"><!-- function fun(obj) { var element=document.getElementById("method"); element.value=obj; alert(element.value); form1.submit(); } // --></mce:script> </head> <body> <center> <form action="calculator.do" method="get" id="form1"> <input type="text" name="num1"/><br> <input type="text" name="num2"/><br> <input type="hidden" id="method" name="method"> <input type="button" name="add" value="加" onclick="fun('add')"> <input type="button" name="sub" value="减" onclick="fun('sub')"> <input type="button" name="mul" value="乘" onclick="fun('mul')"> <input type="button" name="div" value="除" onclick="fun('div')"> </form> </center> </body> </html>   

 

2.CalculatorForm(传参,验证ActionFrom)

view plain copy to clipboard print ?
  1. package com.feng.struts.form;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import org.apache.struts.action.ActionErrors;  
  5. import org.apache.struts.action.ActionForm;  
  6. import org.apache.struts.action.ActionMapping;  
  7. import org.apache.struts.action.ActionMessage;  
  8.   
  9. public class CalculatorForm extends ActionForm {  
  10.  private double num1;  
  11.  private double num2;  
  12.  private double result;  
  13.   
  14.  public double getNum1() {  
  15.   return num1;  
  16.  }  
  17.   
  18.  public void setNum1(double num1) {  
  19.   this.num1 = num1;  
  20.  }  
  21.   
  22.  public double getNum2() {  
  23.   return num2;  
  24.  }  
  25.   
  26.  public void setNum2(double num2) {  
  27.   this.num2 = num2;  
  28.  }  
  29.   
  30.  public double getResult() {  
  31.   return result;  
  32.  }  
  33.   
  34.  public void setResult(double result) {  
  35.   this.result = result;  
  36.  }  
  37.   
  38.  public ActionErrors validate(ActionMapping mapping,  
  39.    HttpServletRequest request) {  
  40.   ActionErrors errors = new ActionErrors();  
  41.   if ("".equals(num1) || "".equals(num2)) {  
  42.    errors.add("num.null"new ActionMessage("num.num"));  
  43.   }  
  44.   return errors;  
  45.  }  
  46.   
  47.  public void reset(ActionMapping mapping, HttpServletRequest request) {  
  48.   // TODO Auto-generated method stub   
  49.  }  
  50. }  
package com.feng.struts.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; public class CalculatorForm extends ActionForm { private double num1; private double num2; private double result; public double getNum1() { return num1; } public void setNum1(double num1) { this.num1 = num1; } public double getNum2() { return num2; } public void setNum2(double num2) { this.num2 = num2; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ("".equals(num1) || "".equals(num2)) { errors.add("num.null", new ActionMessage("num.num")); } return errors; } public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } }   

3.CalculatorAction(处理类DispatchAction)

view plain copy to clipboard print ?
  1. package com.feng.struts.action;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. import org.apache.struts.action.ActionErrors;  
  7. import org.apache.struts.action.ActionForm;  
  8. import org.apache.struts.action.ActionForward;  
  9. import org.apache.struts.action.ActionMapping;  
  10. import org.apache.struts.action.ActionMessage;  
  11. import org.apache.struts.action.ActionMessages;  
  12. import org.apache.struts.actions.DispatchAction;  
  13.   
  14. import com.feng.struts.form.CalculatorForm;  
  15.   
  16. public class CalculatorAction extends DispatchAction {  
  17.   
  18.  public ActionForward add(ActionMapping mapping, ActionForm form,  
  19.    HttpServletRequest request, HttpServletResponse response) {  
  20.   CalculatorForm calculatorForm = (CalculatorForm) form;  
  21.   calculatorForm.setResult(calculatorForm.getNum1()  
  22.     + calculatorForm.getNum2());  
  23.   return mapping.findForward("ok");  
  24.  }  
  25.   
  26.  public ActionForward sub(ActionMapping mapping, ActionForm form,  
  27.    HttpServletRequest request, HttpServletResponse response) {  
  28.   CalculatorForm calculatorForm = (CalculatorForm) form;  
  29.   calculatorForm.setResult(calculatorForm.getNum1()  
  30.     - calculatorForm.getNum2());  
  31.   return mapping.findForward("ok");  
  32.  }  
  33.   
  34.  public ActionForward mul(ActionMapping mapping, ActionForm form,  
  35.    HttpServletRequest request, HttpServletResponse response) {  
  36.   CalculatorForm calculatorForm = (CalculatorForm) form;  
  37.   calculatorForm.setResult(calculatorForm.getNum1()  
  38.     * calculatorForm.getNum2());  
  39.   return mapping.findForward("ok");  
  40.  }  
  41.   
  42.  public ActionForward div(ActionMapping mapping, ActionForm form,  
  43.    HttpServletRequest request, HttpServletResponse response) {  
  44.   CalculatorForm calculatorForm = (CalculatorForm) form;  
  45.   if (calculatorForm.getNum2() == 0) {  
  46.    ActionErrors errors=new ActionErrors();  
  47. //   errors.add("num", new ActionMessage("num.zero"));   
  48. //   super.saveErrors(request, errors);   
  49.    ActionMessages message=new ActionMessages();  
  50.    message.add("d"new ActionMessage("num.zero"));  
  51.    super.saveErrors(request, message);  
  52.    return mapping.findForward("no");  
  53.   } else {  
  54.    calculatorForm.setResult(calculatorForm.getNum1()  
  55.      / calculatorForm.getNum2());  
  56.    return mapping.findForward("ok");  
  57.   }  
  58.  }  
  59. }  
package com.feng.struts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionErrors; 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; import org.apache.struts.actions.DispatchAction; import com.feng.struts.form.CalculatorForm; public class CalculatorAction extends DispatchAction { public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { CalculatorForm calculatorForm = (CalculatorForm) form; calculatorForm.setResult(calculatorForm.getNum1() + calculatorForm.getNum2()); return mapping.findForward("ok"); } public ActionForward sub(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { CalculatorForm calculatorForm = (CalculatorForm) form; calculatorForm.setResult(calculatorForm.getNum1() - calculatorForm.getNum2()); return mapping.findForward("ok"); } public ActionForward mul(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { CalculatorForm calculatorForm = (CalculatorForm) form; calculatorForm.setResult(calculatorForm.getNum1() * calculatorForm.getNum2()); return mapping.findForward("ok"); } public ActionForward div(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { CalculatorForm calculatorForm = (CalculatorForm) form; if (calculatorForm.getNum2() == 0) { ActionErrors errors=new ActionErrors(); // errors.add("num", new ActionMessage("num.zero")); // super.saveErrors(request, errors); ActionMessages message=new ActionMessages(); message.add("d", new ActionMessage("num.zero")); super.saveErrors(request, message); return mapping.findForward("no"); } else { calculatorForm.setResult(calculatorForm.getNum1() / calculatorForm.getNum2()); return mapping.findForward("ok"); } } }   

4.ApplicationResources.properties(资源文件,显示错误信息)

 

view plain copy to clipboard print ?
  1. num.null=/u6570/u5B57/u4E0D/u80FD/u4E3A/u7A7A/!/!/!  
  2. num.zero=/u6570/u5B57/u4E0D/u80FD/u4E3A/u96F6/!/!/!  
num.null=/u6570/u5B57/u4E0D/u80FD/u4E3A/u7A7A/!/!/! num.zero=/u6570/u5B57/u4E0D/u80FD/u4E3A/u96F6/!/!/!   

5.struts-config.xml(配置文件)

view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">  
  3.   
  4. <struts-config>  
  5.   <data-sources />  
  6.   <form-beans >  
  7.     <form-bean name="calculatorForm" type="com.feng.struts.form.CalculatorForm" />  
  8.   
  9.   </form-beans>  
  10.   
  11.   <global-exceptions />  
  12.   <global-forwards />  
  13.   <action-mappings >  
  14.     <action  
  15.       attribute="calculatorForm"  
  16.       input="/form/calculator.jsp"  
  17.       name="calculatorForm"  
  18.       parameter="method"  
  19.       path="/calculator"  
  20.       scope="request"  
  21.       type="com.feng.struts.action.CalculatorAction" >  
  22.       <forward name="ok" path="/result.jsp"></forward>  
  23.       <forward name="no" path="/error.jsp"></forward>  
  24.       </action>  
  25.   
  26.   </action-mappings>  
  27.   
  28.   <message-resources parameter="com.feng.struts.ApplicationResources" />  
  29. </struts-config>  

你可能感兴趣的:(struts,input,action,div,button,fun)