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

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

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

1.calculator.jsp(计算页面)

  1. <%@pagelanguage="java"pageEncoding="GB18030"%>
  2. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  3. <html>
  4. <head>
  5. <title>calculator.jsp</title>
  6. <mce:scripttype="text/javascript"><!--
  7. functionfun(obj)
  8. {
  9. varelement=document.getElementById("method");
  10. element.value=obj;
  11. alert(element.value);
  12. form1.submit();
  13. }
  14. //--></mce:script>
  15. </head>
  16. <body>
  17. <center>
  18. <formaction="calculator.do"method="get"id="form1">
  19. <inputtype="text"name="num1"/><br>
  20. <inputtype="text"name="num2"/><br>
  21. <inputtype="hidden"id="method"name="method">
  22. <inputtype="button"name="add"value="加"onclick="fun('add')">
  23. <inputtype="button"name="sub"value="减"onclick="fun('sub')">
  24. <inputtype="button"name="mul"value="乘"onclick="fun('mul')">
  25. <inputtype="button"name="div"value="除"onclick="fun('div')">
  26. </form>
  27. </center>
  28. </body>
  29. </html>

2.CalculatorForm(传参,验证ActionFrom)

  1. packagecom.feng.struts.form;
  2. importjavax.servlet.http.HttpServletRequest;
  3. importorg.apache.struts.action.ActionErrors;
  4. importorg.apache.struts.action.ActionForm;
  5. importorg.apache.struts.action.ActionMapping;
  6. importorg.apache.struts.action.ActionMessage;
  7. publicclassCalculatorFormextendsActionForm{
  8. privatedoublenum1;
  9. privatedoublenum2;
  10. privatedoubleresult;
  11. publicdoublegetNum1(){
  12. returnnum1;
  13. }
  14. publicvoidsetNum1(doublenum1){
  15. this.num1=num1;
  16. }
  17. publicdoublegetNum2(){
  18. returnnum2;
  19. }
  20. publicvoidsetNum2(doublenum2){
  21. this.num2=num2;
  22. }
  23. publicdoublegetResult(){
  24. returnresult;
  25. }
  26. publicvoidsetResult(doubleresult){
  27. this.result=result;
  28. }
  29. publicActionErrorsvalidate(ActionMappingmapping,
  30. HttpServletRequestrequest){
  31. ActionErrorserrors=newActionErrors();
  32. if("".equals(num1)||"".equals(num2)){
  33. errors.add("num.null",newActionMessage("num.num"));
  34. }
  35. returnerrors;
  36. }
  37. publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){
  38. //TODOAuto-generatedmethodstub
  39. }
  40. }

3.CalculatorAction(处理类DispatchAction)

  1. packagecom.feng.struts.action;
  2. importjavax.servlet.http.HttpServletRequest;
  3. importjavax.servlet.http.HttpServletResponse;
  4. importorg.apache.struts.action.ActionErrors;
  5. importorg.apache.struts.action.ActionForm;
  6. importorg.apache.struts.action.ActionForward;
  7. importorg.apache.struts.action.ActionMapping;
  8. importorg.apache.struts.action.ActionMessage;
  9. importorg.apache.struts.action.ActionMessages;
  10. importorg.apache.struts.actions.DispatchAction;
  11. importcom.feng.struts.form.CalculatorForm;
  12. publicclassCalculatorActionextendsDispatchAction{
  13. publicActionForwardadd(ActionMappingmapping,ActionFormform,
  14. HttpServletRequestrequest,HttpServletResponseresponse){
  15. CalculatorFormcalculatorForm=(CalculatorForm)form;
  16. calculatorForm.setResult(calculatorForm.getNum1()
  17. +calculatorForm.getNum2());
  18. returnmapping.findForward("ok");
  19. }
  20. publicActionForwardsub(ActionMappingmapping,ActionFormform,
  21. HttpServletRequestrequest,HttpServletResponseresponse){
  22. CalculatorFormcalculatorForm=(CalculatorForm)form;
  23. calculatorForm.setResult(calculatorForm.getNum1()
  24. -calculatorForm.getNum2());
  25. returnmapping.findForward("ok");
  26. }
  27. publicActionForwardmul(ActionMappingmapping,ActionFormform,
  28. HttpServletRequestrequest,HttpServletResponseresponse){
  29. CalculatorFormcalculatorForm=(CalculatorForm)form;
  30. calculatorForm.setResult(calculatorForm.getNum1()
  31. *calculatorForm.getNum2());
  32. returnmapping.findForward("ok");
  33. }
  34. publicActionForwarddiv(ActionMappingmapping,ActionFormform,
  35. HttpServletRequestrequest,HttpServletResponseresponse){
  36. CalculatorFormcalculatorForm=(CalculatorForm)form;
  37. if(calculatorForm.getNum2()==0){
  38. ActionErrorserrors=newActionErrors();
  39. //errors.add("num",newActionMessage("num.zero"));
  40. //super.saveErrors(request,errors);
  41. ActionMessagesmessage=newActionMessages();
  42. message.add("d",newActionMessage("num.zero"));
  43. super.saveErrors(request,message);
  44. returnmapping.findForward("no");
  45. }else{
  46. calculatorForm.setResult(calculatorForm.getNum1()
  47. /calculatorForm.getNum2());
  48. returnmapping.findForward("ok");
  49. }
  50. }
  51. }

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

  1. num.null=/u6570/u5B57/u4E0D/u80FD/u4E3A/u7A7A/!/!/!
  2. num.zero=/u6570/u5B57/u4E0D/u80FD/u4E3A/u96F6/!/!/!

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

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.2//EN""http://struts.apache.org/dtds/struts-config_1_2.dtd">
  3. <struts-config>
  4. <data-sources/>
  5. <form-beans>
  6. <form-beanname="calculatorForm"type="com.feng.struts.form.CalculatorForm"/>
  7. </form-beans>
  8. <global-exceptions/>
  9. <global-forwards/>
  10. <action-mappings>
  11. <action
  12. attribute="calculatorForm"
  13. input="/form/calculator.jsp"
  14. name="calculatorForm"
  15. parameter="method"
  16. path="/calculator"
  17. scope="request"
  18. type="com.feng.struts.action.CalculatorAction">
  19. <forwardname="ok"path="/result.jsp"></forward>
  20. <forwardname="no"path="/error.jsp"></forward>
  21. </action>
  22. </action-mappings>
  23. <message-resourcesparameter="com.feng.struts.ApplicationResources"/>
  24. </struts-config>

你可能感兴趣的:(Dispatch)