准备工作先要有2个配置文件+1个jar包
配置文件1:urlrewrite.xml
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite>
<rule>
<note>
<!-- 这是一个通用请求url rewrite
将请求forword url加上".action"的处理器上。例:
请求 http://{domain}/user/login 将被forward到 http://{domain}/user/login.do
请求 http://{domain}/user/login/ 将被forward到 http://{domain}/user/login.do
请求 http://{domain}/user/logout/ 将被forward到 http://{domain}/user/logout.do
-->
</note>
<from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)/?.html$</from>
<to type="forward">/$1.action</to>
</rule>
<!--
<rule>
<note>这是一个通用请求url rewrite</note>
<from>^/([a-z0-9A-Z_]+)/([a-z0-9A-Z_]+)/!([a-z0-9A-Z_]+).html$</from>
<to type="forward">/$2.action?id=$1</to>
</rule>
-->
<!-- Override default validation.js from WebWork -->
<rule>
<from>^/struts/css_xhtml/validation.js$</from>
<to type="forward">/template/css_xhtml/validation.js</to>
</rule>
<!-- The following fixes a bug in XFire: -->
<!-- http://jira.codehaus.org/browse/XFIRE-1089 -->
<rule>
<from>^//services/(.*)$</from>
<to type="forward">/services/$1</to>
</rule>
</urlrewrite>
配置文件2:web.xml
在web.xml文件中插入一个filter
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
1个jar包:urlrewritefilter-3.1.0.jar
但是这样配置会遇到问题
因此需要在web.xml文件中添加一个东西,完整的filter链如下:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<!-- Struts2 filter -->
<filter>
<filter-name>struts2-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts2-cleanup</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1、在web.xml中EncodingFilter的位置应该在Struts2的FilterDispatcher之前,道理很简单,要先调整字符集,再进入Action。
2、如果使用Urlrewrite,要指定filter-mapping的dispatcher方式,如下
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
3、在做上传文件的时候,要在web.xml中增加ActionContextCleanUp这个filter,如果不增加,会发生第一次上传取不到文件的情况
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
按照Struts2的API,filter的顺序是
struts-cleanup filter
SiteMesh filter
FilterDispatcher