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="myPackge" namespace="/" extends="struts-default">
<action name="pluralAction" class="com.Action.pluralAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
pluralAction
package com.Action;
import com.opensymphony.xwork2.ActionSupport;
public class pluralAction extends ActionSupport {
private String first1;
private String check1;
private String first2;
private String check;
private String second1;
private String check2;
private String second2;
private String res1;
private String check3;
private String res2;
private String info;
public String getFirst1() {
return first1;
}
public void setFirst1(String first1) {
this.first1 = first1;
}
public String getCheck1() {
return check1;
}
public void setCheck1(String check1) {
this.check1 = check1;
}
public String getFirst2() {
return first2;
}
public void setFirst2(String first2) {
this.first2 = first2;
}
public String getCheck() {
return check;
}
public void setCheck(String check) {
this.check = check;
}
public String getSecond1() {
return second1;
}
public void setSecond1(String second1) {
this.second1 = second1;
}
public String getCheck2() {
return check2;
}
public void setCheck2(String check2) {
this.check2 = check2;
}
public String getSecond2() {
return second2;
}
public void setSecond2(String second2) {
this.second2 = second2;
}
public String getRes1() {
return res1;
}
public void setRes1(String res1) {
this.res1 = res1;
}
public String getCheck3() {
return check3;
}
public void setCheck3(String check3) {
this.check3 = check3;
}
public String getRes2() {
return res2;
}
public void setRes2(String res2) {
this.res2 = res2;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String execute() throws Exception {
//第一个复数
String first1 = getFirst1();
String check1 = getCheck1();
String first2 = getFirst2();
//运算符
String check = getCheck();
//第二个复数
String second1 = getSecond1();
String check2 = getCheck2();
String second2 = getSecond2();
if (isNumeric(first1) && isNumeric(first2) && isNumeric(second1) && isNumeric(second2)) {
if (check.equalsIgnoreCase("+")) {
res1 = Integer.parseInt(first1) + Integer.parseInt(second1) + "";
check3 = "+";
res2 = Integer.parseInt(first2) + Integer.parseInt(second2) + "";
} else if (check.equalsIgnoreCase("-")) {
res1 = Integer.parseInt(first1) - Integer.parseInt(second1) + "";
check3 = "-";
res2 = Integer.parseInt(first2) - Integer.parseInt(second2) + "";
} else if (check.equalsIgnoreCase("*")) {
res1 = Integer.parseInt(first1) * Integer.parseInt(second1) - Integer.parseInt(second2) * Integer.parseInt(first2) + "";
check3 = "+";
res2 = Integer.parseInt(first2) * Integer.parseInt(second1) + Integer.parseInt(second2) * Integer.parseInt(first1) + "";
} else if (check.equalsIgnoreCase("/")) {
int x1 = Integer.parseInt(first1) * Integer.parseInt(second1) + Integer.parseInt(second2) * Integer.parseInt(first2);
int x2 = Integer.parseInt(first2) * Integer.parseInt(second1) - Integer.parseInt(first1) * Integer.parseInt(second2);
int x3 = Integer.parseInt(second1) * Integer.parseInt(second1) + Integer.parseInt(second2) * Integer.parseInt(second2);
res1 = x1 / x3 + "";
check3 = "-";
res2 = x2 / x3 + "";
}
info = "计算结果为:" + res1 + check3 + res2 + "i";
} else {
info = "您输入的格式错误";
}
return SUCCESS;
}
private static boolean isNumeric(String str) {
for (int i = str.length(); --i >= 0; ) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
private static String isMore(String test) {
if (Integer.parseInt(test) < 0) {
return "-";
} else {
return "+";
}
}
}
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: Elijah
Date: 2018/9/27
Time: 19:21
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="pluralAction">
<s:label value="请输入第一个复数" theme="simple"/><br/>
<s:textfield name="first1" theme="simple"/>+<s:textfield name="first2" theme="simple"/>i<br/>
<br/>
<s:select list="{'+','-','*','/'}" name="check" label="运算符" theme="simple"/><br/><br/>
<s:label value="请输入第二个复数" theme="simple"/><br/>
<s:textfield name="second1" theme="simple"/>+<s:textfield name="second2" theme="simple"/>i<br/>
<s:submit value="计算"/>
</s:form>
<br>
<hr width="100%">
<s:property value="info"/>
</body>
</html>