Struts2的快速入门

1. Struts2框架的概述

  • Struts2是流行和成熟的基于MVC设计模式的Web应用程序框架。 Struts2不只是Struts1下一个版本,它是一个完全重写的Struts架构。

2. Struts2的快速入门

1.去官网下载Struts2的开发环境包

Struts2的官网 https://struts.apache.org
在这里插入图片描述

进去点击下载下面的这个就好了
Struts 2.5.20–>包含所有文件的这个
struts-2.5.20-all.zip (65MB) [PGP] [SHA256]

2.解压

解压 struts-2.5.20-all.zip
会出现四个大目录,这里不截图了,自己解压看自己电脑。

  • apps : 存放官方提供的struts2实例程序
  • docs : 存放官方提供的文档,快速入门,API文档等
  • lib :存放struts2的核心类库以及第三方的jar包,灰常重要。
  • src :存放的是Struts2的源代码。
3.搭建环境
  • 1.首先我们要创建一个web项目

  • 2.导入jar包
    在这里插入图片描述

  • asm-3.3.jar :操作Java字节码的类库

  • asm-commons.jar :提供了基于事件的表现形式

  • asm-tree-3.3.jar : 提供了基于事件的表现形式

  • struts2-core.jar :struts2的核心类库

  • xwork-core.jar : WebWork核心库,Struts2的构建基础

  • ognl.jar :对象图导航语言

  • freemarker.jar :struts2标签模板使用的类库

  • javassist.jar :JavaScript字节码解释器

  • commons-fileupload.jar :文件上传组建包

  • commons-io.jar :输入输出依赖包

  • commons-lang.jar :数据类型工具

  • log4j-api.jar :日志组建APi

  • log4j-core.jar :日志组建依赖包

4.创建页面
  • 1.创建一个名为demo1的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Struts2的入门</h1>
	<h3>
		<a href="${ pageContext.request.contextPath }/hello.action">
		Struts2的入门</a>
	</h3>
</body>
</html>
  • 2.编写一个Action页面,注意包名
package struts.demo1;

//Struts2的入门action类
public class HelloAction {
	/**
	 * 提供一个方法: 
	 * 方法签名固定的 共有的 
	 * 返回值是String类型 
	 * 方法名execute 
	 * 在这个方法中不能传递参数。
	 */
	public String execute() {
		System.out.println("HelloAction执行了...");
		return "success";
	}
}

  • 3.在struts.xml完成Action的配置
    • 这个文件配置是放在src下面的
<?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>
	<!-- Struts2为了管理Action的配置,通过包来进行管理, -->
	<!--下面来配置Struts2的包 -->
	<package name="demo1" extends="struts-default" namespace="/">
		<!-- 配置Action的名称以及路径 -->
		<action name="hello" class="struts.demo1.HelloAction">
			<!--配置页面的跳转 -->
			<result name="success">/demo1/success.jsp</result>
		</action>
	</package>
</struts>

  • 4.在web.xml中配置核心过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<!-- 配置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>
</web-app>
  • 5.写一个名为success.jsp页面
    • 和demo1.jsp在同一个目录下
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>跳转成功页面!!!</h1>
</body>
</html>
  • 6.以上已经完成一个入门教程。可以测试了。
3.配置struts.xml文件提示(在不联网的情况下)

首先打开window–>preference–>xml catalog
在这里面配置。

4.Struts2的执行流程
  • 当用户访问某个Action类的时候,先经过核心过滤器,在过滤器中执行一组拦截器(期间实现部分功能),然后执行目标Action类,执行其中的方法,最后根据Action类的返回值,进行页面跳转。
5.Struts2配置文件的加载顺序
  • 后配置的常量的值会覆盖先配置的常量的值。
6.package相关配置
<?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="demo1" extends="struts-default" namespace="/">
	<action name="hello" class="struts.demo1.HelloAction" >
		<result name="success">/demo1/success.jsp</result>
	</action>
</package>	
</struts>
*** package标签称为包,这个包与Java中的包的概念不一致,这个包是为了更好的管理Action的配置。
*** package标签的四个属性
  ** name :包的名称,在一个项目中不重名即可。
  ** extends :继承哪个包,通常值为struts-default
  ** abstract : 抽象的,用于其他抽象包的继承 
  ** namespace :名称空间,与<action>标签中的name属性共同决定用户访问的路径
     * 名称空间有三种写法
     * 带名称的名称空间  :namespace="/aaa"
     * 根名称空间       : namespace="/"
     * 默认名称空间.    : namespace=""
     * 如果同时配置,优先访问带名称的名称空间
  
*** action标签的配置是用来配置Action类
*** action 标签的四个属性
  ** name : 与namespace共同决定用户访问路径
  ** class :Action 类的全路径
  ** method : 执行Action中哪个方法的方法名,默认值是execute
  ** converter : 用于设置类型转换器
7.Struts2中常量的配置
  • 在Struts2的框架中,在default.properties中定义了有很多的常量。
  • 如何修改一些常量的值的三种方式
***例如以下———>修改默认扩展名:
1.在struts.xml中进行修改
  <constant name="struts.action.extension" value="action"/>	
2.在struts.properties中进行修改
  struts.action.extension=action
3.在web.xml中进行修改
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>xyz</param-value> 
</init-param>
8. 分模块开发的配置
  • include的配置
<?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>
	<!-- 配置Struts2的常量 -->
	<constant name="struts.action.extension" value="action"/>

	<include file="struts/demo1/struts_demo1.xml"/>
	<include file="struts/demo2/struts_demo2.xml"/>
	<include file="struts/demo3/struts_demo3.xml"/>
</struts>
9.Action类的创建与常见写法
  • Action类是POJO(Plain Ordinary Java Object 简单的Java对象)的类
//Action的编写方式:Action类是一个POJO的类
public class ActionDemo1 {
public String execute(){
	System.out.println("ActionDemo1执行了...");
	return null;
}
}
  • Action类实现一个Action的接口
/**
* Action的编写方式二:实现一个Action的接口
* * 实现接口的这种方式:提供了五个常量(五个逻辑视图的名称)
* 		* SUCCESS	:成功(可以修改)
* 		* ERROR		:失败(可以修改)
* 		* LOGIN		:登录出错页面跳转
* 		* INPUT		:表单校验的时候出错(不可以修改)
* 		* NONE=null	:不跳转
*/
public class ActionDemo2 implements Action{

@Override
public String execute() throws Exception {
	System.out.println("ActionDemo2执行了...");
	return NONE;
}
  • Action类继承ActionSupport类
  • ActionSupport类本身实现了Action接口以及其他类的接口,为用户提供更多的功能。
import com.opensymphony.xwork2.ActionSupport;
/**
* Action的编写方式三:Action类继承ActionSupport类
* 推荐使用继承ActionSupport方式
* ActionSupport中提供了数据校验、国际化等一系列操作的方法。
*/
public class ActionDemo3 extends ActionSupport{

@Override
public String execute() throws Exception {
	System.out.println("ActionDemo3执行了...");
	return NONE;
}
}

10.Action类的访问方法
  • 1.通过method设置
  • 2.通过通配符的方式进行配置
    • name属性中使用的*代表任意字符,method中{1}代表name属性中出现的第一个*所代替的字符,这组通配符是开发中最常用的方式。
  • 3.动态访问方法(过于抽象)
<?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>
<!-- Struts2为了管理Action的配置,通过包进行管理。 -->
<!-- 配置Struts2的包 ================ -->
<package name="demo3" extends="struts-default" namespace="/">

<action name="userFind" class="struts.demo3.UserAction" method="find"></action>
<action name="userUpdate" class="truts.demo3.UserAction" method="update"></action>
<action name="userDelete" class="struts.demo3.UserAction" method="delete"></action>
<action name="userSave" class="struts.demo3.UserAction" method="save"></action>
	
	<!-- 通配符的方式 -->
<action name="product_*" class="struts.demo3.ProductAction" method="{1}"></action>
	
	<!-- 动态方法访问的方式 -->
	<!-- 开启动态方法访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
	<action name="customer" class="struts.demo3.CustomerAction"></action>
</package>	
</struts>

<h1>Action的访问</h1>
<h3>通过method方式</h3>
<a href="${pageContext.request.contextPath}/userFind.action">查询用户</a>
<a href="${pageContext.request.contextPath}/userUpdate.action">修改用户</a>
<a href="${pageContext.request.contextPath}/userDelete.action">删除用户</a>
<a href="${pageContext.request.contextPath}/userSave.action">保存用户</a>

<h3>通过通配符的方式</h3>
<a href="${pageContext.request.contextPath}/product_find.action">查询商品</a>
<a href="${pageContext.request.contextPath}/product_update.action">修改商品</a>
<a href="${pageContext.request.contextPath}/product_delete.action">删除商品</a>
<a href="${pageContext.request.contextPath}/product_save.action">保存商品</a>

<h3>通过动态方法访问的方式</h3>
<a href="${pageContext.request.contextPath}/customer!find.action">查询客户</a>
<a href="${pageContext.request.contextPath}/customer!update.action">修改客户</a>
<a href="${pageContext.request.contextPath}/customer!delete.action">删除客户</a>
<a href="${pageContext.request.contextPath}/customer!save.action">保存客户</a>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值