Struts2框架项目介绍

Struts2框架

Struts2是经典的javaEE的web框架,提供了基于mvc应用程序的开发模式。

(一)搭建Struts2环境

  • 新建Dynamic Web project工程
  • 导入jar包
  • 在配置文件web.xml中配置struts2的启动信息(如下)

<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>

  • 在src目录下添加struts.xml 

(二)创建web程序步骤

  • 创建工程,搭建环境
  • 设计模型层(JavaBean类),设计有关的业务逻辑处理——大多为具体的实体类
  • 设计控制层(Action类),实现模型和视图间的交互
  • 设计视图层(jsp页面),实现信息的提交与显示
  • 修改映射文件struts.xml,在其中添加有关的Action和视图类之间的映射

例如:求两数之和 

(1)结构图如下:

(2)代码如下:

(1)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>c03_01_struts2add</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
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)AddAction.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)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="-">negative.jsp</result>
            <result name="+">positive.jsp</result>           
      </action>     
    </package>
</struts>

(5)input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<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>

(6)positive.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%>
<html>
<head>
    <title>计算结果为大于等于0的页面</title>
</head>
<body>
     代数和为非负整数,其和值为:${add.x}+${add.y}=${add.sum}<br>
</body>
</html>

(7)negative.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
   <title>计算结果为负数的页面</title>
</head>
<body>
	代数和为负整数,其和值为:${add.x}+${add.y}=${add.sum}	
</body>
</html>
发布了136 篇原创文章 · 获赞 54 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Flora_SM/article/details/103795092