第二天(3)计算器

/day32/WebRoot/calculator.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
      <script type="text/javascript" src="js/jquery-1.8.2.js"></script>
      <script type="text/javascript">
          $(function(){
              $("form").submit(function(){
                  var flag = false;
                  if(validateNum1() && validateNum2()){
                      flag = true;
                  }    
                  return flag;
              });
          });
          function validateNum1(){
            var flag = false;
            var num1 = $(":text:first").val();
            num1 = $.trim(num1);
            if(num1!=null && num1.length>0){
                flag = true;
            }else{
                alert("操作数一必填");
                $(":text:first").focus();            
            }
            return flag;          
          }
          function validateNum2(){
              var flag = false;
            var num2 = $(":text:last").val();
            num2 = $.trim(num2);
            if(num2!=null && num2.length>0){
                flag = true;
            }else{
                alert("操作数二必填");
                $(":text:last").focus();            
            }
              return flag;
          }
      </script>
  </head>
  <body>
      <form 
          action="/day32/calculator" 
          method="post">
          <table border="2" align="center">
              <caption><h3>四则运算</h3></caption>
              <tr>
                  <th>第一个操作数</th>
                  <td><input type="text" name="num1" maxlength="4" value="${requestScope.num1}"/></td>
                  <td>
                      <s:fielderror fieldName="num1"/>
                  </td>
              </tr>
              <tr>
                  <th>操作符</th>
                  <td>
                      <select name="opt">
                          <option value="+" ${requestScope.opt=='+'?'selected':''}>+</option>
                          <option value="-" ${requestScope.opt=='-'?'selected':''}>-</option>
                          <option value="*" ${requestScope.opt=='*'?'selected':''}>*</option>
                          <option value="/" ${requestScope.opt=='/'?'selected':''}>/</option>
                      </select>
                  </td>
              </tr>
              <tr>
                  <th>第二个操作数</th>
                  <td><input type="text" name="num2" maxlength="4" value="${requestScope.num2}"/></td>
                <td>
                      <s:fielderror fieldName="num2"/>
                  </td>              
              </tr>
              <tr>
                  <td colspan="2" align="center">
                      <input type="submit" value="计算"/>
                  </td>
              </tr>
          </table>
      </form>
      <hr/>
      <c:if test="${requestScope.flag=='showResult'}">
          ${requestScope.num1}
          ${requestScope.opt} 
          ${requestScope.num2} 
          ${requestScope.equ}
          ${requestScope.result}<br/>
      </c:if>
      ${requestScope.message}
  </body>
</html>

/day32/src/cn/itcast/web/struts2/work/CalculatorAction.java

package cn.itcast.web.struts2.work;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

//计算
public class CalculatorAction extends ActionSupport{
    private Integer num1;//操作数一
    private String opt;//操作符
    private Integer num2;//操作数二
    public Integer getNum1() {
        return num1;
    }
    public void setNum1(Integer num1) {
        this.num1 = num1;
    }
    public String getOpt() {
        return opt;
    }
    public void setOpt(String opt) {
        this.opt = opt;
    }
    public Integer getNum2() {
        return num2;
    }
    public void setNum2(Integer num2) {
        this.num2 = num2;
    }
    public void validateExecute() {
        String strNum1 = this.num1+"";
        String strNum2 = this.num2+"";
        if(strNum1!=null && strNum1.trim().length()>0){
            //只能输入1位到4位的数字,包括1位和4位
            if(strNum1.matches("^[0-9]{1,4}$")){
                ;
            }else{
                this.addFieldError("num1","操作数一必须是四位数字");
            }
        }else{
            this.addFieldError("num1","操作数一必填");    
        }
        if(strNum2!=null && strNum2.trim().length()>0){
            if(strNum2.matches("^[0-9]{1,4}$")){
                ;
            }else{
                this.addFieldError("num2","操作数一必须是四位数字");
            }
        }else{
            this.addFieldError("num2","操作数一必填");    
        }
    }
    public String execute() throws Exception {
        ActionContext.getContext().put("flag","showResult");
        if(this.opt.equals("+")){
            this.result = this.num1 + this.num2;
        }else if(this.opt.equals("-")){
            this.result = this.num1 - this.num2;
        }else if(this.opt.equals("*")){
            this.result = this.num1 * this.num2;
        }else if(this.opt.equals("/")){
            if(this.num2!=0){
                this.result = this.num1 / this.num2;
            }else{
                ActionContext.getContext().put("message","除数不能为0");
            }
        }
        return this.SUCCESS;
    }
    private Integer result;//结果
    private String equ = "=";//等号
    public Integer getResult() {
        return result;
    }
    public String getEqu() {
        return equ;
    }
}

/day32/src/cn/itcast/web/struts2/work/struts_work.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="workPackage" extends="struts-default" namespace="/">
        <action 
            name="calculator" 
            class="cn.itcast.web.struts2.work.CalculatorAction" 
            method="execute">
            <result name="success" type="dispatcher">
                /calculator.jsp
            </result>
            <result name="input" type="dispatcher">
                /calculator.jsp
            </result>
        </action>
    </package>
</struts>  

 

posted on 2016-07-28 18:36  step_step  阅读(177)  评论(1编辑  收藏  举报

导航