Struts2计算三角形周长面积

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>

猜你喜欢

转载自blog.csdn.net/weixin_40330033/article/details/82873428