Struts框架 实现复数加减操作

实现方式:领域对象的Action类设计与属性驱动传参

所用到的 jar 包

在这里插入图片描述

1.配置 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Struts Blank</display-name>
	<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>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

2.编写领域类

package bean;

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 result() {
		if (ima > 0) {
			return real + "+" + ima + "i";
		} else
			return real + "" + ima + "i";
	}
}

3.action

package action;

import bean.*;

public class ComplexAction {
	private double real1;
	private double ima1;
	private String oper;
	private double real2;
	private double ima2;
	private String temp = null;

	public ComplexAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	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 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).result();
		} else if ("-".equals(oper)) {
			this.temp = comp1.sub(comp2).result();
		} else if ("*".equals(oper)) {
			this.temp = comp1.mul(comp2).result();
		} else
			this.temp = comp1.div(comp2).result();
		return flag;
	}
}

4.配置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>

action的name属性的值应与jsp中的保持一致

5. input.jsp页面、output.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="fushujisuan" method="post">
		实部1:<input type="text" name="real1"><br> 虚部1:<input
			type="text" name="ima1"><br> 运算类型:<br> <select
			name="oper">
			<option>+</option>
			<option>-</option>
			<option>*</option>
			<option>/</option>
		</select><br> 实部2:<input type="text" name="real2"><br> 虚部2:<input
			type="text" name="ima2"><br> <input type="submit"
			value="提 交"><br>
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${temp}
</body>
</html>
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页