工程结构
需要导入的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"/> <s:property value="check"/> <s:property value="num2"/> = <s:property value="num3"/><br>
</body>
</html>