Stuts2 使用DispatchAction做一个简单的计算器
使用DispatchAction做一个简单的计算器
DispatchAction是Struts中Action中的一种,为一种动态Action,可以处理相同类型的方法
1.calculator.jsp(计算页面)
- <%@ 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)
- 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)
- 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(资源文件,显示错误信息)
- num.null=/u6570/u5B57/u4E0D/u80FD/u4E3A/u7A7A/!/!/!
- num.zero=/u6570/u5B57/u4E0D/u80FD/u4E3A/u96F6/!/!/!
5.struts-config.xml(配置文件)
- <?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>
- </action>
- </action-mappings>
- <message-resources parameter="com.feng.struts.ApplicationResources" />
- </struts-config>