urlrewriter 和 Struts2 整合

urlrewriter 和 Struts2 整合

准备工作先要有2个配置文件+1个jar包

配置文件1:urlrewrite.xml

写道
<?xml version="1.0" encoding="UTF-8"?>
<!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

写道
<!-- UrlRewriteFilter 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链如下:

写道
<!-- UrlRewriteFilter 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值