web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Fushujisuan_Struct2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="fushujisuan" class="Action.ComplexAction" method="calComplex">
<result name="success">/Output.jsp</result>
</action>
</package>
</struts>
Javabean.Complex.java
package JavaBean;
public class Complex {
private double real;
private double ima;
public Complex(double real, double ima) {
super();
this.real = real;
this.ima = ima;
}
public Complex() {
super();
// TODO Auto-generated constructor stub
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getIma() {
return ima;
}
public void setIma(double ima) {
this.ima = ima;
}
public Complex add(Complex a) {
return new Complex(this.real+a.real,this.ima+a.ima);
}
public Complex sub(Complex a) {
return new Complex(this.real-a.real,this.ima-a.ima);
}
public Complex mul(Complex a) {
double x = this.real * a.real - this.ima * a.ima;
double y = this.real * a.ima + this.ima * a.real;
return new Complex(x,y);
}
public Complex div(Complex a) {
double z = a.real*a.real+a.ima*a.ima;
double x = (this.real * a.real + this.ima * a.ima)/z;
double y = (this.real * a.ima - this.ima * a.real)/z;
return new Complex(x,y);
}
public String info() {
if(ima>0) {
return real+"+"+ima+"i";
}else return real+""+ima+"i";
}
}
Action.ComplexAction.java
package Action;
import JavaBean.*;
public class ComplexAction {
private double real1;
private double ima1;
private String oper;
private double real2;
private double ima2;
private String temp = null;
public ComplexAction(double real1, double ima1, String oper, double real2, double ima2, String temp) {
super();
this.real1 = real1;
this.ima1 = ima1;
this.oper = oper;
this.real2 = real2;
this.ima2 = ima2;
this.temp = temp;
}
public ComplexAction() {
super();
// TODO Auto-generated constructor stub
}
public double getReal1() {
return real1;
}
public void setReal1(double real1) {
this.real1 = real1;
}
public double getIma1() {
return ima1;
}
public void setIma1(double ima1) {
this.ima1 = ima1;
}
public String getOper() {
return oper;
}
public void setOper(String oper) {
this.oper = oper;
}
public double getReal2() {
return real2;
}
public void setReal2(double real2) {
this.real2 = real2;
}
public double getIma2() {
return ima2;
}
public void setIma2(double ima2) {
this.ima2 = ima2;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String calComplex() throws Exception{
String flag = "success";
Complex comp1 = new Complex(this.real1,this.ima1);
Complex comp2 = new Complex(this.real2,this.ima2);
if("+".equals(oper)) {
this.temp=comp1.add(comp2).info();
}else if("-".equals(oper)) {
this.temp=comp1.sub(comp2).info();
}else if("*".equals(oper)) {
this.temp=comp1.mul(comp2).info();
}else
this.temp=comp1.div(comp2).info();
return flag;
}
}
Input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>数据输入页面</title>
</head>
<body>
<form action="fushujisuan" method="post">
请输入第一个复数的实部:<input type="text" name="real1"><br>
请输入第一个复数的虚部:<input type="text" name="ima1"><br>
请选择运算类型:<br>
<select name="oper">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select><br>
请输入第二个复数的实部:<input type="text" name="real2"><br>
请输入第二个复数的虚部:<input type="text" name="ima2"><br>
<input type="submit" value="提 交"><br>
</form>
</body>
</html>
Output.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>输出结果页面</title>
</head>
<body>
运算结果等于:${temp}
</body>
</html>
输入界面:
输出界面: