在开发过程中需要用到url重写的功能,以实现对url的访问管理。能想到的时使用UrlRewriteFilter来实现,web.xml文件中的配置如下:
<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>log4j</param-value> </init-param> </filter><filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>配置文件只是有了一个执行环境,我们还需要一个执行的说明书,就是我们得按照什么来进行url重写,需要在WEB-INF下放上urlrewrite.xml这样的文件,这个文件里配置的就是一些重写规则,格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<urlrewrite>
<!-- rewrite Example -->
<!--rule> <from>/games/(.*)</from> <to>/game.do?id=$1</to> </rule -->
<!-- ======================================================================
http://game.com/server/serverId == http://game.com/server.do?id=serverId
http://game.com/serverlist == http://game.com/serverlist.do
=========================================================================== -->
<!--rule>
<from>/site/serverlist</from>
<to>/site/ServerList.do</to>
</rule-->
<outbound-rule>
<note>
The outbound-rule specifies that when response.encodeURL is called (if you are using
JSTL c:url)
the url /rewrite-status will be rewritten to /test/status/.
The above rule and
this outbound-rule means that end users should never see the
url /rewrite-status only
/test/status/ both in thier location bar and in hyperlinks
in your pages.
</note>
<from>/rewrite-status</from>
<to>/test/status/</to>
</outbound-rule>
</urlrewrite>具体的配置还得项目开发完成之后再进行具体配置。