JSP学习笔记(二十九):利用urlrewrite重写url
第一步
下载urlrewrite,官方下载地址:http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip
第二步
解压缩文件,压缩包内文件copy到项目中(压缩包位置 -> 项目位置):
urlrewrite-2.6.0-src/webapp/WEB-INF/lib/urlrewrite-2.6.0.jar -> WebRoot/WEB-INF/lib/urlrewrite-2.6.0.jar
urlrewrite-2.6.0-src/webapp/WEB-INF/urlrewrite.xml -> WebRoot/WEB-INF/urlrewrite.xml
第三步
修改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>
<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>
如果项目中应用了struts2,还需要修改struts2的相关设置(修改部分用绿色字标示):
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
注意:struts2过滤器应放到urlrewrite过滤器之后
相关知识点:xml里<filter-mapping>中的<dispatcher>作用 http://hintcnuie.javaeye.com/blog/226251
第四步
修改urlrewrite.xml文件,添加对应的文件映射规则,就可以实现url重写的功能了。
下载的压缩包里urlrewrite-2.6.0-src/manual/index.html 有详细的介绍和例子。