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>
相關文章
- 7.實現加減
- 如何用位運算實現整數的加減法
- 【jquery】實現購物車加減jQuery
- 位運算實現加減乘除
- 直播商城APP,直接實現購物車商品數量加減APP
- 不用加減乘除做加法(Java實現)Java
- ACM 分數加減法ACM
- Verilog實現加減乘除運算
- JavaScript加減乘數運算JavaScript
- 【實驗】【總結】Oracle日期類操作(格式 加減乘 取毫秒)Oracle
- 加減密和數字簽名
- 如何使用struts框架來實現樹型選單?框架
- ABAP面試問題 - 不使用加減乘除等操作比較兩個整數大小面試
- 超大整數的加減乘除計算方法
- Trick:處理加減等差數列的技巧
- java 日期加減天數、月數、年數的計算方式Java
- PHP日期加減月數,天數,週數,小時,分,秒等等PHP
- JavaScript浮點數加減乘除精確計算JavaScript
- 計算機組成原理浮點數加減計算機
- 33:計算分數加減表示式的值
- 三個數字的加減乘除模運算
- 浮點數的加減乘除運算細節
- 用連結串列的方式實現大數相減-Java實現Java
- 用單連結串列實現多項式加,減,乘,簡單微分
- python實現時間的加減,類似linux的date命令PythonLinux
- golang 時間加減Golang
- java時間加減Java
- 自定義View加減View
- ORACLE時間加減Oracle
- 高精度加減乘
- JavaScript中任意兩個數加減的解決方案JavaScript
- C++筆記:輸入輸出、變數、變數加減乘除C++筆記變數
- struts實現上傳篇
- struts實現下載篇
- Redis 如何實現庫存扣減操作和防止被超賣?Redis
- 妙用 CSS 動畫來實現顏色加深、減淡等混合操作CSS動畫
- C語言實現時間的加一天或者減一天C語言
- 基於Gor實現流量複製(加middleware功能增強)Go