标签:
UrlRewriteFilter是一个改写URL的Java Web过滤器,可见将动态URL静态化。适用于任何Java Web服务器(Resin,Jetty,JBoss,Tomcat,Orion等)。与其功能类似的还有Apache的mod_rewrite。
将动态URL转化为伪静态URL的好处主要有三个:
官网地址:http://tuckey.org/urlrewrite/
官方文档:http://tuckey.org/urlrewrite/#documentation
http://urlrewritefilter.googlecode.com/files/urlrewritefilter-4.0.3.jar
<dependency> <groupId>org.tuckey</groupId> <artifactId>urlrewritefilter</artifactId> <version>4.0.3</version> </dependency>
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <!--<init-param> <param-name>confReloadCheckInterval</param-name> <param-value>60</param-value> </init-param> <init-param> <param-name>confPath</param-name> <param-value>/WEB-INF/urlrewrite.xml</param-value> </init-param>--> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <!--<dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher>--> </filter-mapping>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"> <urlrewrite> <rule> <note>.../book/1234 instead of .../book.jsp?id=1234</note> <from>/book/([0-9]+)$</from> <to>/book.jsp?id=$1</to> </rule> <rule match-type="wildcard"> <!--match-type默认为regex--> <from>/blog/archive/**</from> <to type="redirect">/roller/history/$1</to><!--type默认为forward--> </rule> </urlrewrite>
根元素为urlrewrite;至少有一个rule元素;每个rule元素有且只有一对from和to元素;rule元素中可以有任意个condition/run/set元素,包含condition元素时所有条件都满足才会映射url和执行set。
每个rule的转换过程伪代码:
Pattern.compile(<from> element); pattern.matcher(request url); matcher.replaceAll(<to> element); if ( <condition> elements match && matcher.find() ) { handle <set> elements (if any) execute <run> elements (if any) perform <to> element (if any) }
http://cdn.rawgit.com/paultuckey/urlrewritefilter/master/src/doc/manual/4.0/index.html#filterparams
标签:
原文地址:http://www.cnblogs.com/pixy/p/4987080.html