urlRewrite

  • Post author:
  • Post category:其他


熟悉apache的朋友都应该对apache的urlwrite的强大功能很熟悉,呵呵,今天的主角不是apache,而是urlRewriteFilter。 一个基于过滤器的java实现。

Based on the popular and very useful mod_rewrite for apache, 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.

urlrewrite的好处就不多说了,这里简单说一下配置。

安装:

1. 下载lib文件解压缩到web目录下,目前的版本是1.2。下载地址:http://tuckey.org/urlrewrite/dist/urlrewritefilter-1.2.zip

2. 编辑 WEB-INF/web.xml 加入下面的配置(filter-mapping自定义)

<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>

3. 在WEB-INF目录中创建规则配置文件urlrewrite.xml.

4. 重启应用环境.

简单吧。具体来看几个地方

web.xml::

<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>logLevel</param-name>

<param-value>DEBUG</param-value>

</init-param>

<init-param>

<param-name>statusEnabled</param-name>

<param-value>true</param-value>

</init-param>

<init-param>

<param-name>statusPath</param-name>

<param-value>/status</param-value>

</init-param>

</filter>

1) confReloadCheckInterval : 设置检查,加载配置文件的时间间隔,0或空表示永远不检查重新加载

2) logLevel: 日志记录level

3) statusEnabled:

4)statusPath:

urlrewrite.xml:跳转规则的配置文件,具体请参考UrlRewriteFilter DTD (Document Type Definition).

规则定义采用正则表达式(Perl5 style),具体参考Jakarta ORO’s的说明文档

例子:

<rule>

<from>/products/([0-9]+)</from>

<to>/products/index.jsp?product_id=$1</to>

</rule>

http://localhost/example/products/1 将会更具规则重定向到 http://localhost/example/products/index.jsp?product_id=1



版权声明:本文为wujiapeng2009原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。