Struts2計算三角形周長面積
阿新 • • 發佈:2018-11-03
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <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> </web-app>
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="myPackge" extends="struts-default" namespace="/"> <action name="count" class="com.Action.CountAction"> <result name="success">/result.jsp</result> <result name="error">/index.jsp</result> </action> </package> </struts>
CountAction
package com.Action; import com.opensymphony.xwork2.ActionSupport; public class CountAction extends ActionSupport { private String first; private String second; private String third; private double perimeter; private double area; private String info; public String getFirst() { return first; } public void setFirst(String first) { this.first = first; } public String getSecond() { return second; } public void setSecond(String second) { this.second = second; } public String getThird() { return third; } public void setThird(String third) { this.third = third; } public double getPerimeter() { return perimeter; } public void setPerimeter(double perimeter) { this.perimeter = perimeter; } public double getArea() { return area; } public void setArea(double area) { this.area = area; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } // 計算周長 public static double countPerimeter(double x1, double x2, double x3) { return x1 + x2 + x3; } // 計算面積 public static double countArea(double x1, double x2, double x3) { double p = (x1 + x2 + x3) / 2; return Math.sqrt(p * (p - x1) * (p - x2) * (p - x3)); } @Override public String execute() throws Exception { double x1 = Double.parseDouble(getFirst()); double x2 = Double.parseDouble(getSecond()); double x3 = Double.parseDouble(getThird()); String ret = ERROR; if (x1 + x2 > x3 && x1 + x3 > x2 && x2 + x3 > x1) { info = "計算結果為:"; perimeter = Math.round(countPerimeter(x1, x2, x3) * 100) / 100; area = Math.round(countArea(x1, x2, x3) * 100) / 100; ret = SUCCESS; } else { info = "您輸入的三條邊不滿足三角形條件"; } return ret; } }
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: Elijah
Date: 2018/9/27
Time: 17:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>計算三角形周長和麵積</title>
</head>
<body>
<body>
<s:form action="count">
<s:label value="請輸入三角形的三條邊長"/>
<s:textfield name="first" label="第一條邊"/>
<s:textfield name="second" label="第二條邊"/>
<s:textfield name="third" label="第三條邊"/>
<s:submit value="計算"/>
</s:form>
<s:property value="info"/>
</body>
</body>
</html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: Elijah
Date: 2018/9/27
Time: 17:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>計算結果</title>
</head>
<body>
<s:property value="info"/>
周長:
<s:property value="perimeter"/>
面積:
<s:property value="area"/>
</body>
</html>