天天看點

計算三角形面積(采用struts2的MVC模式)

輸入界面(注意input中的name要與模型中的屬性及action中的屬性名一緻)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<link href="form.css" rel="stylesheet" type="text/css">
<style type="text/css">
label{
	display:inline-block;
	padding:3px 6px;
	text-align:right;
	width:200px;
	vertical-align:top;
}
body{ 
			background-repeat: no-repeat;
			background-size: cover;

}
</style>
</head>
<body>
<br><br><br>


<div id="login">
	<div id="form">
<form>
<p><a text-align="center"><font size="6" color="black"><b><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;複數四則運算</i></b></font></a></p><br><br><br>
<font size="2" color="black">
<label >請輸入第一條邊:</label>
<input name="rectangle.a" class="name" type="text" id="first1" size="20" ><br><br>
<label >請輸入第二條邊:</label>
<input name="rectangle.b" class="name" type="text" id="first2" size="20"><br><br>
<label >請輸入第三條邊:</label>
<input name="rectangle.c" class="name" type="text" id="second1" size="20" ><br><br>


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input formaction="opRectangle"  type="submit"   value="求面積" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type="reset" value="重置">
</form>
</div>
</div>

</body>
</html>
           

三角形類

package bean;


public class Rectangle {
	double a,b,c;

	public Rectangle(double a, double b, double c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}

	public Rectangle() {
		super();
	}

	public double getA() {
		return a;
	}

	public void setA(double a) {
		this.a = a;
	}

	public double getB() {
		return b;
	}

	public void setB(double b) {
		this.b = b;
	}

	public double getC() {
		return c;
	}

	public void setC(double c) {
		this.c = c;
	}
	
	public double mj(){
		double p=(a+b+c)/2;
		return Math.sqrt(p*(p-a)*(p-b)*(p-c));
	}
	
	public String toString(){
		return "該三角形面積為"+this.mj();
	}
	
	public boolean flag(){
		if(a+b>c&&b+c>a&&a+c>b)
			return true;
		else return false;
	}
	
	public String YanZheng(){
		String forward=null;
		if(this.flag())
			forward="yes";
		else
			forward="no";
		return forward;
	}
	
}


           

action類

package com.action;
import bean.Rectangle;
public class YanZhengAction {
	private Rectangle rectangle;

	public YanZhengAction() {
		super();
	}

	public YanZhengAction(Rectangle rectangle) {
		super();
		this.rectangle = rectangle;
	}

	public Rectangle getRectangle() {
		return rectangle;
	}

	public void setRectangle(Rectangle rectangle) {
		this.rectangle = rectangle;
	}
	
	public String YanZheng(){
		String forward=null;
		if(rectangle.flag())
			forward="yes";
		else
			forward="no";
		return forward;
	}
}

           

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="default" namespace="/" extends="struts-default">
 <action name="opRectangle" class="bean.Rectangle" method="YanZheng">
 	<result name="yes">right.jsp</result>
 	<result name="no">wrong.jsp</result>
 	</action>
 </package>
    
</struts>

           

兩個輸出界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
該三邊符合三角形三邊長,三角形面積為:${rectangle.mj() }
</body>
</html>
           
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
該三邊構不成三角形
</body>
</html>

           

我還嘗試了把action類省略,把驗證函數寫到Rectangle類中,xml中進行相應改變,依然可以實作功能