【JAVAEE】Strust2案例1-计算任意两个数据的和
本文最后更新于 1551 天前,其中的信息可能已经有所发展或是发生改变。

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

[sourcecode language=”xml” title=”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>[/sourcecode]

2.编写模型类Add.java。
com/model/Add.java

[sourcecode language=”java” title=”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;
}
}[/sourcecode]

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

[sourcecode language=”java” title=”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;
}
}[/sourcecode]

4.在src目录下创建struts.xml文件
struts.xml

[sourcecode language=”xml” title=”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>[/sourcecode]

5.编写JSP页面。
input.jsp

[sourcecode language=”html” title=”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>[/sourcecode]

negative.jsp

[sourcecode language=”html” title=”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>[/sourcecode]

positive.jsp

[sourcecode language=”html” title=”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>[/sourcecode]

6.导入Struts2必须的jar包
下载文件放入WebContent/WEB-INF/lib文件下并设置好jar包导入
[bdbtn]https://pan.benzhu.xyz/%E4%BB%A3%E7%A0%81/%E6%BA%90%E4%BB%A3%E7%A0%81/jar%E5%8C%85/Struts2%E5%BF%85%E9%9C%80jar%E5%8C%85.rar[/bdbtn]

注意:此版本为Strut-2.3.24,不支持JDK9.0,支持1.8。
eclipse项目下载:[bdbtn]https://pan.benzhu.xyz/%E4%BB%A3%E7%A0%81/%E6%BA%90%E4%BB%A3%E7%A0%81/%E9%A1%B9%E7%9B%AE/Strust2%E6%A1%88%E4%BE%8B1-%E8%AE%A1%E7%AE%97%E4%BB%BB%E6%84%8F%E4%B8%A4%E4%B8%AA%E6%95%B0%E6%8D%AE%E7%9A%84%E5%92%8C.rar[/bdbtn]

效果图:

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇