版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36146275/article/details/69387670
使用struts之前需要配置好环境才能使用,简单的说struts就是一个fiter过滤器。
一、首先我们需要在struts-apache的官网上把struts下载回来【www.struts.apache.org】
解压后的目录如下:
进入apps目录,将war文件解压,待用
二、在eclipse新建Dynamic工程,项目结构如下:
第一步:导入支持struts的jar包
将上面解压出来的文件中的【apps\struts2-rest-showcase\WEB-INF\lib】lib下的所有jar包copy到DynamicWeb工程WEB-INF的lib包下
第二步:web.xml文件是在创建工程时自动创建的
需要在web.xml配置struts的控制器,也就是过滤器。
在此目录【apps\struts2-rest-showcase\WEB-INF】找到web.xml打开编辑
复制<filter>、<filter-mapping>标签的内容到工程的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>struts2Demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置struts的核心控制器,就是过滤器 -->
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- END SNIPPET: filter -->
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第三步:在src下新建一个名为struts.xml的文件
在此目录下【struts-2.5.10.1-all\struts-2.5.10.1\apps\struts2-rest-showcase\WEB-INF\classes】找到struts.xml文件打开复制里面的内容到工程的struts.xml文件,删掉暂时用不到的配置,得到如下信息:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
</struts>
三、检查struts配置是否成功
启动Tomcat查看日志是否出现报错,否,则配置完成。