Struts2
前言
在一些老旧项目里面struts2框架很常见,所有遇到老旧项目时很有必要学习一下一、下载地址
Struts2 的官方下载网址为 http://struts.apache.org
二、使用步骤
1.新建工程
- 在 idea 中新建一个名为 struts2Demo01 的 Web 项目
- 引入相关jar包
- 配置 Struts2 的核心过滤器
打开项目的 web.xml 文件,在文件中添加 Struts2 的核心过滤器 [org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter]
<?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_4_0.xsd"
version="4.0">
<!-- 配置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>
注意:
在 web.xml 文件中, 标签中配置的信息就是 Struts2 的核心过滤器,该过滤器的名称为 struts2,过滤器类为 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,而 标签中配置的是该过滤器的映射。
需要注意的是,在 Struts2.1 版本之前,所使用的核心过滤器类是 org.apache.struts2.dispatcher.FilterDispatcher,从 Struts2.1 版本之后,这个过滤器类已经不推荐使用了,而是使用 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 类。
- 创建 Action 类
在项目的 src 目录下,新建一个名称为 com.demo.action 的包,在包中创建一个名为 HelloWorldAction 的类,并使其继承 ActionSupport 类(Struts2 框架所提供的类)。在 HelloWorldAction 类中定义一个 execute() 方法,返回值为 SUCCESS,编辑后的代码如下所示。
import com.opensymphony.xwork2.ActionSupport;
/**
* @author 等风来
* @date 2021年12月02日
*/
public class HelloWorldAction extends ActionSupport {
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
在上述 HelloWorldAction.java 中,execute() 方法的返回值 SUCCESS 是从父类中继承的常量字段,用于表示执行成功,并返回结果页面。execute() 方法的返回值对应 struts.xml 文件中 标签中 name 属性的值,Struts2 框架会根据 name 属性值执行对应处理结果下的视图资源。
- 编写 Struts2 的配置文件
在 src 目录下新建一个名称为 struts.xml 的文件
报红也不要管
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定 Struts2 配置文件的 DTD 信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
<!-- Struts2的Action必须放在指定的包空间下定义 -->
<package name="hello" namespace="/" extends="struts-default">
<!-- 定义 action,该 action 对应的类为 com.mengma.action.HelloWorldAction 类-->
<action name="helloWorld" class="com.mengma.action.HelloWorldAction">
<!-- 定义处理结果和视图资源之间的映射关系 -->
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
在 struts.xml 文件中, 标签中定义了请求路径,以及与其对应的 Action 类的映射关系,子标签 定义了处理结果和视图资源之间的映射关系。关于 struts.xml 文件的详细配置信息,后面的教程中会进行重点讲解,此处读者可不必关心其细节问题。
在 Struts2 的配置文件需要编写的内容有很多。尤其是文件上面的 dtd 信息,如果手动编写的话十分容易出错。同样的,在解压的 struts2-blank 文件夹中,将 WEB-INF/classes 目录下的 struts.xml 文件复制到自己所建立的 Web 项目的 src 目录下,然后在此基础上进行修改和配置即可。
- 创建视图首页文件
在 WebContent 目录下创建一个名称为 index.jsp 的页面文件,在其中编写一个超链接,用于访问 Action 对象,此链接指向的地址为 helloWorld.action,如下所示。
<%@ 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>
<title>首页</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/helloWorld.action">
第一个 Struts2 程序!
</a>
</body>
</html>
- 创建返回文件
在 WebContent 下再创建一个名称为 success.jsp 的文件,作为 Action 对象处理成功后的返回页面,如下所示
<%@ 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>
<title>成功页面</title>
</head>
<body>
您的第一个小程序执行成功,欢迎来到Struts2的世界!
</body>
</html>
- 启动项目并查看结果
2.原理
在客户端浏览器上单击 index.jsp 中的超链接时,会发送一个 helloWorld.action 请求,该请求被核心过滤器拦截后,会通过 struts.xml 文件中的配置找到请求对应的 HelloWorldAction,并默认调用 HelloWorldAction 中的 execute() 方法返回逻辑视图名,然后再通过配置文件找到并转发给对应的视图页面 success.jsp 中,最后生成响应内容并输出响应的返回结果。