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>
相關文章
- 位運算實現加減乘除
- 【jquery】實現購物車加減jQuery
- Verilog實現加減乘除運算
- 直播商城APP,直接實現購物車商品數量加減APP
- 浮點數加減法
- JavaScript加減乘數運算JavaScript
- Struts框架_9 Struts2的驗證框架
- 79、概述struts,以及struts如何實現MVC架構的?MVC架構
- ABAP面試問題 - 不使用加減乘除等操作比較兩個整數大小面試
- Trick:處理加減等差數列的技巧
- 超大整數的加減乘除計算方法
- 用連結串列的方式實現大數相減-Java實現Java
- 浮點數的加減乘除運算細節
- 三個數字的加減乘除模運算
- 計算機組成原理浮點數加減計算機
- JavaScript浮點數加減乘除精確計算JavaScript
- 用單連結串列實現多項式加,減,乘,簡單微分
- Redis 如何實現庫存扣減操作和防止被超賣?Redis
- struts動態多檔案上傳實現
- (三)struts2進階之實現Action
- 大數模擬 加減乘除 判斷大數是否為素數 板子
- 高精度加減乘
- golang 時間加減Golang
- Svelte入門——Web Components實現跨框架元件複用Web框架元件
- Java之struts2框架學習Java框架
- JavaScript中任意兩個數加減的解決方案JavaScript
- SSH三大框架的搭建整合(Spring+Hibernate+Struts2)實現增刪改查登入框架Spring
- Struts2防止表單重複提交
- C++筆記:輸入輸出、變數、變數加減乘除C++筆記變數
- 妙用 CSS 動畫來實現顏色加深、減淡等混合操作CSS動畫
- 使用Robot機器人框架實現自動化操作機器人框架
- shell加減乘除運算
- 【oracle】日期加減計算Oracle
- Struts2框架快速入門筆記框架筆記
- 複製快捷鍵ctrl加什麼怎麼操作 電腦鍵盤複製貼上快捷鍵是crtl加什麼
- DIY 實現 ThinkPHP 核心框架 (十三)利用反射實現引數繫結PHP框架反射
- struts:實現圖片的上傳 argument type mismatch errorError
- Python 複數屬性及操作介紹Python