1. 程式人生 > >Struts2實現建議計算器

Struts2實現建議計算器

工程結構

需要匯入的jar包

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="first" extends="struts-default">
        <action name="jisuan" class="com.Action.DealAction">
            <result name="success">/result.jsp</result>
            <result name="index">/index.jsp</result>
        </action>
    </package>
</struts>

DealAction

package com.Action;

import com.opensymphony.xwork2.ActionSupport;

public class DealAction extends ActionSupport {
   private String num1;         //第一個運算元
   private String num2;         //第二個運算元
   private String check;         //運算子
   private double num3;            //計算結果

   public String getNum1() {
      return num1;
   }

   public void setNum1(String num1) {
      this.num1 = num1;
   }

   public String getNum2() {
      return num2;
   }

   public void setNum2(String num2) {
      this.num2 = num2;
   }

   public String getCheck() {
      return check;
   }

   public void setCheck(String check) {
      this.check = check;
   }

   public double getNum3() {
      return num3;
   }

   public void setNum3(double num3) {
      this.num3 = num3;
   }

   public String execute() {
      String x = getNum1();
      String y = getNum2();

      double num4 = Double.parseDouble(x);
      double num5 = Double.parseDouble(y);
      System.out.println(num4);
      switch (check) {
         case "+":
            num3 = num4 + num5;
            break;
         case "-":
            num3 = num4 - num5;
            break;
         case "*":
            num3 = num4 * num5;
            break;
         case "/":
            num3 = num4 / num5;
            break;
      }
      return SUCCESS;
   }
}

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Elijah
  Date: 2018/9/26
  Time: 17:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>實現簡易計算器</title>
</head>
<body>
<s:form action="jisuan">
    <s:label value="簡易計算器"></s:label>
    <s:textfield name="num1" label="First Number"></s:textfield>
    <s:select name="check" list="{'+','-','*','/'}" label="請選擇運算子"></s:select>
    <s:textfield name="num2" label="Second Number"></s:textfield>
    <s:submit value="計算"></s:submit>
</s:form>
</body>
</html>

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Elijah
  Date: 2018/9/26
  Time: 18:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<html>
<head>
    <title>運算結果</title>
</head>
<body>
<br>您計算的結果是:
<s:property value="num1"/>&nbsp;<s:property value="check"/>&nbsp;<s:property value="num2"/>&nbsp;=&nbsp;<s:property value="num3"/><br>
</body>
</html>