基本介紹
簡介,使用方法,
簡介
UrlRewriteFilter is a Java Web Filter for any J2EE compliant web application server (such as Resin, Orion or Tomcat), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite.
使用方法
方法一:
1、增加依賴:
[pre]<dependency> <groupId>org.tuckey</groupId> <artifactId>urlrewritefilter</artifactId> <version>4.0.3</version></dependency>
2、配置web.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>/*</url-pattern>
</filter-mapping>
根據自己的需要,將相應目錄下的url轉給UrlRewriteFilter來處理。
3、配置urlwrite規則檔案WEB-INF/urlrewrite.xml
http://xxx.xxx/content.jsp?id=1&contentid=404
靜態化為:
http://xxx.xxx/content/1_404.html
配置如下:
<rule>
<from>/content/([0-9]+)_([0-9]+).html</from>
<to>/content.jsp?id=$1&contentid=$2</to>
</rule>
方法二:
1、下載urlrewritefilter-1.1.zip (about 500kb) ,解壓縮後將檔案考到相應的web-inf/lib和web-inf下。
2、配置web.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>/*</url-pattern>
</filter-mapping>
根據自己的需要,將相應目錄下的url轉給UrlRewriteFilter來處理。
3、配置urlwrite規則檔案WEB-INF/urlrewrite.xml
添加自己的urlwrite規則,如將
http://localhost/aaa/bbb.jsp?id=99&name=abc
表現為:
http://localhost/aaa/bbb/99_abc.html
配置如下:
<rule>
<from>/aaa/bbb/([0-9]+)_([a-z]+).html</from>
<to>/../aaa/bbb.jsp?id=$1&name=$2</to>
</rule>