1、简介
UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如Resin,Orion,Tomcat等)。其典型应用就把动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。
其主页:http://tuckey.org/urlrewrite/
2、安装
在其主页下载该包文件,把其jar 包放在lib 目录下,在web.xml 中添加下面内容
Xml代码
- <filter>
- <filter-name>UrlRewriteFilter</filter-name>
- <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>UrlRewriteFilter</filter-name>
- <url-pattern>*</from>
- <to>/world.jsp</to>
- </rule
<outbound-rule>元素
参数 | 取值 | 描述 |
enabled | true(默认)、false | 是否应该该规则 |
encodefirst | false(默认)、false | 是否在执行<outbound-rule>之前执行encodeURL(),ture为之后,false为之前 |
实例:
Xml代码
- <outbound-rule>
- <from>^/world.jsp?country=([a-z]+)&city=([a-z]+)$</from>
- <to>/world/$1/$2</to>
- </outbound-rule>
Java代码
- 使用jsp
- <a href="<%= response.encodeURL("/world.jsp?country=usa&city=nyc") %>">nyc</a>
- 将输出
- <a href="/world/usa/nyc">nyc</a>
- 或者使用jstl 标签
- <a href="<c:url value="/world.jsp?country=${country}&city=${city}" />">nyc</a>
- 将输出
- <a href="/world/usa/nyc">nyc</a>
<name>元素
Xml代码
- <!--该规则的名称,可以用在rule元素和outbound-rule 元素中-->
- lt;rule>
- <name>World Rule</name>
- <from>^/world/([a-z]+)/([a-z]+)$</from>
- <to>/world.jsp?country=$1&city=$2</to>
- </rule>
<note>元素
Xml代码
- <!--用来描述该规则,可用在rule 元素和outbound-rule元素中-->
- lt;rule>
- <name>World Rule</name>
- <note>
- Cleanly redirect world requests to JSP,
- a country and city must be specified.
- </note>
- <from>^/world/([a-z]+)/([a-z]+)$</from>
- <to>/world.jsp</to>
- </rule>
<condition>元素
可以用来为rule元素选择条件,所有条件将在规则执行时执行(除非显式的把“next” 设为“or”)
参数 | 取值 | 描述 |
type | header(默认)、method、port、time等 | 设置一些条件的类型 |
name | 可为任何值 | 如果type 取值为header,这个名称将是http header 的值 |
next | and(默认)、or | and:下一个rule 元素和这个rule 必须匹配。or:下一个rule元素或者这个condition 将被匹配 |
operator | equal(默认)、notequal、greater、less等 | ie请求的值和condition 值比较 |
实例:
Xml代码
- <condition name="user-agent" operator="notequal">Mozilla/[1-4]</condition>
- <condition type="user-in-role" operator="notequal">bigboss</condition>
- <condition name="host" operator="notequal">www.example.com</condition>
- <condition type="method" next="or">PROPFIND</condition>
- <condition type="method">PUT</condition>
<from> 元素
rule 或者outbound-rule 必须至有一个from 元素,该值为url 相对于上下文的值
参数 | 取值 | 描述 |
casesensitive | false(默认)、true | 是否要求该值的大小写,false为大小写匹配,true为忽略大小写 |
<to>元素
重写后的输出值
参数 | 取值 | 描述 |
type | forward(默认)、passthrough、redirect等 | url 的跳转问题 |
last | false(默认)、true | false:余下的rule 元素将被执行,如果该规则符合的话 true:剩下的rule 元素不被执行,如果该规则符合的话 |
encode | false(默认)、true | response.encodeURL([to]) 是否被调用。 false:将在url重写前调用。 true:将不被调用 |
context | 如果应用服务器配置允许 cross context(跨越上下文),这个属性将被forward(只有forward可以,redirct 或者其他to元素的类型都不可以) 比如在tomcat 的配置文件中设有: <Context docBase="app" path="/app" reloadable="true" crossContext="true"/> |
<to>null</to>表示当这个规则匹配时,将不会有任何的反应。
to元素可以包含后引用(backreferences)和变量
Backreferences
比如: %N
Provides access to the grouped parts (parentheses) of the pattern from the last matched Condition in the current rule. N must be less than 10 and greater than 0 (i.e. %1, %2, %3 etc).
(上面不理解,未翻译)
变量
%{varName}
任何变量的 condition type 可以被用来作为varName。比如:%{port}将被翻译为80 ,%{year}将被翻译成2009等
函数
%{function:params}
函数可以用在set元素或者to元素中。
名称 | 实例 | 输入结果 |
---|---|---|
replace | | my dog is a blue dog |
replaceFirst | | my cat is a blue dog |
escape | | a+b+c |
unescape | | a b c |
lower | | hello world |
upper | | HELLO |
trim | | abc def |
set元素
如果rule 匹配的话,允许你设置一些变量。
参数 | 取值 | 描述 |
type | request、session、cookie、charset等 | 设置域的类型 |
name | 任何数 | 在request、session、response-header、cookie,有特殊的作用 |
实例:
Xml代码
- <!--把client 的值设进request中,可通过request.getAttribute("client")来获取-->
- <rule>
- <condition name="user-agent">Mozilla/3/.0 (compatible; AvantGo .*)</from>
- <from>.*</from>
- <set name="client">AvantGo</set>
- </rule>
- <rule>
- <condition name="user-agent">UP/.Browser/3.*SC03 .* </from>
- <from>.*</from>
- <set name="client">Samsung SCH-6100</set>
- </rule>
<run> 元素
当rule和它的condition匹配时,你可以运行Object中的一个方法
参数 | 取值 | 描述 |
class | 你所要调用的方法的Object | |
method | run(默认) | 你所要调用的方法,该方法必须带有(HttpServletRequest, HttpServletResponse)参数。例如:run(HttpServletRequest request, HttpServletResponse response) 如果init(ServletConfig) 或者 |
neweachtime | false(默认)、true | 该Oject 是否为单例执行。false为单例,true为每次都new 新的Object |
实例:
Java代码
- <rule>
- <from>^/world/[a-z]+/[a-z]+$</from>
- <run class="com.blah.web.WorldServlet" method="doGet" />
- <to>/world-presentation.jsp</to>
- </rule>
- lt;!--可以设置一些初始值-->
- <run class="com.blah.web.MyServlet" method="doGet">
- <init-param>
- <param-name>someParamName</param-name>
- <param-value>10</param-value>
- </init-param>
- </run>
才此方法中出现的异常将会被包装成ServletException 后抛出。
<class-rule>元素
每次请求都执行这个方法在rule 元素中,具体的例子在org.tuckey.web.filters.urlrewrite.sample可以查看。
参数 | 取值 | 描述 |
class | 运行的Object,必须带有包的全名 | |
method | matches(默认) | 运行的方法,和run元素中的方法类似 |
last | true(默认) | 当为false时更多的rule将被执行,即使寂静匹配 |
实例:
Xml代码
- <class-rule class="com.blah.web.MyRuleClass" />
4、小知识点
(1)xml 的一些字符必须要进行转义,如& 要转成 $amp;
(2)正则表达式里 在from 元素中,是以 ^ 开始 $结束的。例如请求:/my/url/path 将不能匹配
<from>^/url/$</from>
但能匹配
<from>/url/</from>
(3)如果你用了<outbound-rule> 元素,记得在页面输出的时候进行重写。即 jstl 表达式或者Java脚本输出
(4)如果你发现正则表达式比较难表达,可以用wildcards 表达式来写。
5、wildcard 表达式匹配方法
用wildcard 可以取代正则表达式,要使用该表达式的时候记得要在rule 元素中 把match-type 设为 wildcard ,因为默认是使用正则表达式的。
实例:
/big/url/*
匹配
/big/url/abc.html
不匹配
/big/url/abc/dir/
or
/big/url/abc/
/big/url/**
匹配
/big/url/abc.html
,
/big/url/abc/dir/
和
/big/url/abc/
实例:
/my/big/url/*
匹配
/my/big/url/abc.html
和
$1
将被设为
abc.html