使用DispatchAction做一个简单的计算器
<%@ 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="加" οnclick="fun('add')"> <input type="button" name="sub" value="减" οnclick="fun('sub')"> <input type="button" name="mul" value="乘" οnclick="fun('mul')"> <input type="button" name="div" value="除" οnclick="fun('div')"> </form> </center> </body> </html>
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 } }
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"); } } }
num.null=/u6570/u5B57/u4E0D/u80FD/u4E3A/u7A7A/!/!/! num.zero=/u6570/u5B57/u4E0D/u80FD/u4E3A/u96F6/!/!/!
DispatchAction是Struts中Action中的一种,为一种动态Action,可以处理相同类型的方法
1.calculator.jsp(计算页面)
- <%@pagelanguage="java"pageEncoding="GB18030"%>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <title>calculator.jsp</title>
- <mce:scripttype="text/javascript"><!--
- functionfun(obj)
- {
- varelement=document.getElementById("method");
- element.value=obj;
- alert(element.value);
- form1.submit();
- }
- //--></mce:script>
- </head>
- <body>
- <center>
- <formaction="calculator.do"method="get"id="form1">
- <inputtype="text"name="num1"/><br>
- <inputtype="text"name="num2"/><br>
- <inputtype="hidden"id="method"name="method">
- <inputtype="button"name="add"value="加"οnclick="fun('add')">
- <inputtype="button"name="sub"value="减"οnclick="fun('sub')">
- <inputtype="button"name="mul"value="乘"οnclick="fun('mul')">
- <inputtype="button"name="div"value="除"οnclick="fun('div')">
- </form>
- </center>
- </body>
- </html>
2.CalculatorForm(传参,验证ActionFrom)
- packagecom.feng.struts.form;
- importjavax.servlet.http.HttpServletRequest;
- importorg.apache.struts.action.ActionErrors;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionMapping;
- importorg.apache.struts.action.ActionMessage;
- publicclassCalculatorFormextendsActionForm{
- privatedoublenum1;
- privatedoublenum2;
- privatedoubleresult;
- publicdoublegetNum1(){
- returnnum1;
- }
- publicvoidsetNum1(doublenum1){
- this.num1=num1;
- }
- publicdoublegetNum2(){
- returnnum2;
- }
- publicvoidsetNum2(doublenum2){
- this.num2=num2;
- }
- publicdoublegetResult(){
- returnresult;
- }
- publicvoidsetResult(doubleresult){
- this.result=result;
- }
- publicActionErrorsvalidate(ActionMappingmapping,
- HttpServletRequestrequest){
- ActionErrorserrors=newActionErrors();
- if("".equals(num1)||"".equals(num2)){
- errors.add("num.null",newActionMessage("num.num"));
- }
- returnerrors;
- }
- publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){
- //TODOAuto-generatedmethodstub
- }
- }
3.CalculatorAction(处理类DispatchAction)
- packagecom.feng.struts.action;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importorg.apache.struts.action.ActionErrors;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionForward;
- importorg.apache.struts.action.ActionMapping;
- importorg.apache.struts.action.ActionMessage;
- importorg.apache.struts.action.ActionMessages;
- importorg.apache.struts.actions.DispatchAction;
- importcom.feng.struts.form.CalculatorForm;
- publicclassCalculatorActionextendsDispatchAction{
- publicActionForwardadd(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse){
- CalculatorFormcalculatorForm=(CalculatorForm)form;
- calculatorForm.setResult(calculatorForm.getNum1()
- +calculatorForm.getNum2());
- returnmapping.findForward("ok");
- }
- publicActionForwardsub(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse){
- CalculatorFormcalculatorForm=(CalculatorForm)form;
- calculatorForm.setResult(calculatorForm.getNum1()
- -calculatorForm.getNum2());
- returnmapping.findForward("ok");
- }
- publicActionForwardmul(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse){
- CalculatorFormcalculatorForm=(CalculatorForm)form;
- calculatorForm.setResult(calculatorForm.getNum1()
- *calculatorForm.getNum2());
- returnmapping.findForward("ok");
- }
- publicActionForwarddiv(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse){
- CalculatorFormcalculatorForm=(CalculatorForm)form;
- if(calculatorForm.getNum2()==0){
- ActionErrorserrors=newActionErrors();
- //errors.add("num",newActionMessage("num.zero"));
- //super.saveErrors(request,errors);
- ActionMessagesmessage=newActionMessages();
- message.add("d",newActionMessage("num.zero"));
- super.saveErrors(request,message);
- returnmapping.findForward("no");
- }else{
- calculatorForm.setResult(calculatorForm.getNum1()
- /calculatorForm.getNum2());
- returnmapping.findForward("ok");
- }
- }
- }
4.ApplicationResources.properties(资源文件,显示错误信息)
- num.null=/u6570/u5B57/u4E0D/u80FD/u4E3A/u7A7A/!/!/!
- num.zero=/u6570/u5B57/u4E0D/u80FD/u4E3A/u96F6/!/!/!
5.struts-config.xml(配置文件)
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.2//EN""http://struts.apache.org/dtds/struts-config_1_2.dtd">
- <struts-config>
- <data-sources/>
- <form-beans>
- <form-beanname="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">
- <forwardname="ok"path="/result.jsp"></forward>
- <forwardname="no"path="/error.jsp"></forward>
- </action>
- </action-mappings>
- <message-resourcesparameter="com.feng.struts.ApplicationResources"/>
- </struts-config>