直接貼代碼,需要解釋的是我用的是當前最新的版本struts-2.3.20,配置過濾器之前的版本應該使用org.apache.struts2.dispatcher.FilterDispatcher但是在最新版本里可以看到:
Deprecated.
Since Struts 2.1.3, use
StrutsPrepareAndExecuteFilter
instead or
StrutsPrepareFilter
and
StrutsExecuteFilter
if needing using the
ActionContextCleanUp
filter in addition to this one所以過濾器採用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,以前的也是可以用的,但是這裏遵循最新版本要求
需要引入jar包:
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 過濾器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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="struts" extends="struts-default">
<action name="login" class="cn.com.baiwen.action.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
項目結構圖: