Stuts2 use Dispatchaction to make a simple calculator

Source: Internet
Author: User
Tags mul reset stub
UseDispatchaction make a simple calculator

Dispatchaction is one of the actions in struts and is a dynamic action that can handle the same type of method

1.calculator.jsp (Calculation page)

  View plain Copy to clipboard print? <%@ page language= "java"  pageencoding= "GB18030"%>      <! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >   < html>     

2.CalculatorForm (reference, Verification actionfrom)

View plain Copy to clipboard print? 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;      &NBSP;PUBLIC&NBSP;DOUBLE&NBSP;GETNUM1 ()  {     return num1;    }        public void setnum1 (double num1)  {      this.num1 = num1;    }      &NBSP;PUBLIC&NBSP;DOUBLE&NBSP;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     }  }   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 d Ouble GetNum1 () {return num1;} public void SetNum1 (double num1) {this.num1 = NUM1;} public double getNum2 () {return n um2; public void setNum2 (double num2) {this.num2 = num2,} public double GetResult () {return result;} public void Setresul T (double result) {This.result = result;} publicActionerrors 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}}  &nbs P

3.CalculatorAction (processing class dispatchaction)

View plain Copy to clipboard print? 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,     &nbsp 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 (""));   //   super.saveerrors (request, errors);       actionmessages message=new actionmessages ();       message.add ("D",  new actionmessage (""));      super.saveerrors (request, message);      return mapping.findforward ("no");      } else {      calculatorform.setresult (CALCULATORFORM.GETNUM1 ()     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;/&NBSP;CALCULATORFORM.GETNUM2 ());      return  mapping.findforward ("OK");     }    }  }   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, Ht Tpservletrequest 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 error S=new actionerrors (); Errors.add ("num", New Actionmessage ("")); Super.saveerrors (request, errors); Actionmessages message=new actionmessages (); Message.add ("D", New Actionmessage ("")); Super.saveerrors (request, message); Return Mapping.findforward ("no"); } else {Calculatorform.setresult (CALCULATORFORM.GETNUM1 ()/calculatorform.getnum2 ()); return Mapping.findforward (" OK "); }}} (resource file, displaying error message)

View plain Copy to clipboard print?   num.null=/u6570/u5b57/u4e0d/u80fd/u4e3a/u7a7a/!/!/!!/!/! num.null=/u6570/u5b57/u4e0d/u80fd/u4e3a/u7a7a/!/!/!!/!/!

5.struts-config.xml (config file)

View plain Copy to clipboard print? <?xml version= "1.0"  encoding= "UTF-8"?>   <! doctype struts-config public  "-//apache software foundation//dtd struts  configuration 1.2//en " " HTTP://STRUTS.APACHE.ORG/DTDS/STRUTS-CONFIG_1_2.DTD ">       <struts-config>     <data-sources />     < form-beans >       <form-bean name= "CalculatorForm"  type= " Com.feng.struts.form.CalculatorForm " />        </form-beans>         <global-exceptions />     <global-forwards  />     <action-mappings >       <action          attribute= "CalculatorForm"           input= "/form/calculator.jsp"          name= "CalculatorForm"          parameter= "method"          path= "/ Calculator "         scope=" request "          type= "Com.feng.struts.action.CalculatorAction"  >          <forward name= "OK"  path= "/result.jsp" ></forward>          <forward name= "No"  path= "/error.jsp" ></forward>          </<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.