输入界面(注意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> 复数四则运算</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>
<input formaction="opRectangle" type="submit" value="求面积" >
<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中进行相应改变,依然可以实现功能
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)