您的位置:首页 > 其它

URL(基础二) UrlRewriteFilter的使用

2014-05-14 15:13 295 查看
1.下载urlrewrite,官方下载地址:http://tuckey.org/urlrewrite/

2.非maven项目:将urlrewritefilter-4.0.3.jar复制到WEB-INF/lib里

如果是maven项目,添加依赖:

如:

<!--引入URL插件 -->

<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.3</version>
</dependency>


3.将以下代码添加到web.xml里,路径为: /webapp/WEB-INF/web.xml

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>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>


4.新建一个urlrewrite.xml,路径为:/webapp/WEB-INF/urlrewrite.xml

<?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">
<!--
Configuration file for UrlRewriteFilter http://www.tuckey.org/urlrewrite/ -->
<urlrewrite>
<rule>
<note>
The rule means that requests to /test/status/ will be redirected to /rewrite-status
the url will be rewritten.
</note>
<from>/test/status/</from>
<to type="redirect">%{context-path}/rewrite-status</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>
<!--
INSTALLATION
in your web.xml add...
<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>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
EXAMPLES
Redirect one url
<rule>
<from>/some/old/page.html</from>
<to type="redirect">/very/new/page.html</to>
</rule>
Redirect a directory
<rule>
<from>/some/olddir/(.*)</from>
<to type="redirect">/very/newdir/$1</to>
</rule>
Clean a url
<rule>
<from>/products/([0-9]+)</from>
<to>/products/index.jsp?product_id=$1</to>
</rule>
eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.
Browser detection
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<from>/some/page.html</from>
<to>/some/page-for-old-browsers.html</to>
</rule>
eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.
Centralised browser detection
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<set type="request" name="browser">moz</set>
</rule>
eg, all requests will be checked against the condition and if matched
request.setAttribute("browser", "moz") will be called.
-->
</urlrewrite>


其中<rule></rule>标签是url重写规则,我们需要根据实际情况,进行修改。

<from></from>标签是显示出来的地址,<to></to>标签是映射的实际地址

参数:$1是重写参数,可以为多个,()里是匹配的正则表达式.

如:

<rule>
<from>^/newsInfo-list-([0-9]+).shtml$</from>
<to>/NewsInfo.do?method=list&mod_id=$1</to>
</rule>
<outbound-rule>
<from>/NewsInfo.do\?method=list&mod_id=([0-9]+)$</from>
<to>/newsInfo-list-$1.shtml</to>
</outbound-rule>


5.JSP页面使用:

<c:url var="url_1001001000" value="/NewsInfo.do?method=list&mod_id=1001001000" />
<li><a href="${url_1001001000}">测试地址</a></li>


官网文档中提供如下使用方式:

Xml代码


Using the example above JSP's with the code

<a href="<%= response.encodeURL("/world.jsp?country=usa&city=nyc") %>">nyc</a>

will output

<a href="/world/usa/nyc">nyc</a>

Or JSTL

<a href="<c:url value="/world.jsp?country=${country}&city=${city}" />">nyc</a>

will output

<a href="/world/usa/nyc">nyc</a>

Note, If you are using JSTL (ie, <c:url) this will work also.

6、基本原理

Xml代码


jsp页面地址--> 服务器filter过滤 --> 调用urlrewrite.xml映射规则

-->服务器响应 --> 转换成伪地址

7、在项目中新建index.jsp,启动tomcat,输入http://localhost:8080/webapp/index/1 (注:webapp是项目名称)
实际上访问的是http://localhost:8080/webapp/index.jsp?tid=1 ,这样就简单的实现了伪静态的效果

在ajax当中,var url = "";这个地方写个虚拟的访问路径,

然后将解析规则用正则表示到urlrewrite.xml当中,那么到后台的时候,就可以自动解析成真实的路径,从而达到对url的保护。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: