一只倔强的笨猪

【JAVAEE】Strust2案例1-计算任意两个数据的和

1.配置web.xml文件
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>benzhu1</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.编写模型类Add.java。
com/model/Add.java

package com.model;

public class Add {
	double x; //第1个加数
	double y; //第2个加数
	double sum; //和值
	
	//构造方法
	public Add() {
		this.x = 0;
		this.y = 0;	
	}
	//带参数的构造方法
	public Add(double x, double y) {		
		this.x = x;
		this.y = y;		
	}
	public double getX() {
		return x;
	}
	public void setX(double x) {
		this.x = x;
	}
	public double getY() {
		return y;
	}
	public void setY(double y) {
		this.y = y;
	}
	public double getSum() {
		return x+y;
	}
	public void setSum(double sum) {
		this.sum = sum;
	}	
}

3.设计业务控制类(Action类)。
com/action/Addaciton.java

package com.action;

import com.model.Add;

public class Addaction {
	private Add add; // 业务模型对象属性

	// 不带参数的默认构造方法
	public Addaction() {
	}

	public Add getAdd() {
		return add;
	}

	public void setAdd(Add add) {
		this.add = add;
	}

	// 求值Action方法,并返回"-"或“+”
	public String executeAdd() throws Exception {
		String forward = null;
		if (add.getSum() < 0) {
			forward = "-";
		} else {
			forward = "+";
		}
		return forward;
	}
}

4.在src目录下创建struts.xml文件
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="opadd" class="com.action.Addaction" method="executeAdd">
        <result name="+">positive.jsp</result>
        <result name="-">/negative.jsp</result> 
      </action>

    </package>
</struts>

5.编写JSP页面。
input.jsp

<%@ 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>两数之和计算</title>
</head>
<body>
<form action="opadd" method="post">
		<table>
			<tr>
				<th colspan="2">请输入两个实数:</th>
			</tr>
			<tr>
				<td align="right">加数:</td>
				<td><input type="text" name="add.x" /></td>
			</tr>
			<tr>
				<td align="right">被加数:</td>
				<td><input type="text" name="add.y" /></td>
			</tr>
			<tr>
				<td align="right"><input type="submit" value="求和" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

negative.jsp

<%@ 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>计算结果为负数的页面</title>
</head>
<body>
代数和为负整数,其和值为:${add.x}+${add.y}=${add.sum}
</body>
</html>

positive.jsp

<%@ 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>计算结果为大于等于0的页面</title>
</head>
<body>
代数和为非负整数,其和值为:${add.x}+${add.y}=${add.sum}<br>
</body>
</html>

6.导入Struts2必须的jar包
下载文件放入WebContent/WEB-INF/lib文件下并设置好jar包导入
本地下载

注意:此版本为Strut-2.3.24,不支持JDK9.0,支持1.8。
eclipse项目下载: 本地下载

效果图:

《【JAVAEE】Strust2案例1-计算任意两个数据的和》

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注