准备工作

1.去官网(https://struts.apache.org/)下载struts2.
我下载的版本是struts-2.5.22-all(2020.3.23)
2.Eclipse JavaEE

搭建Struts2的环境

1.加入 jar 包: 复制 \struts-2.5.22-all\struts-2.5.22\lib 下的所有 jar 包到当前 web 应用的 lib 目录下.
2.在 web.xml 文件中配置 struts2: 

<?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">
<display-name>struts2-2</display-name>
<!-- 配置 Struts2 的 Filter -->
<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>
3.在项目的src文件夹下新建一个名为struts.xml的文件,这是struts2的配置文件:

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

</struts>
4.如果是第一次配置文件,Eclipse对struts2没有提示信息显示,所以要添加DTD约束:
打开windows-->preferences-->xml-->xml catalog-->add
在Catalog Entry中的
Key type:选择URL
Key输入http://struts.apache.org/dtds/struts-2.3.dtd
然后点File System
找到struts-2.5.22-all\struts-2.5.22\src\core\src\main\resources下的struts-2.3.dtd
选中,确定就行了

struts2的环境搭建_java