java web url重写

本人新手最近在做项目文章模块的时候,在分页和显示文章时候,总需要通过<a href="asdjfka?id=2">传递一个参数id。在url地址栏要显示传递的参数,所以使用了UrlRewriteFilter来重写url :

首先在:http://tuckey.org/urlrewrite/#download 现在UrlRewriteFilter包,将urlRewriteFilter.jar放到WEB-INF下的lib文件下

在web.xml中添上:

1   <!-- URL过滤器过滤所有url -->
2   <filter>
3          <filter-name>UrlRewriteFilter</filter-name>
4          <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
5   </filter>
6   <filter-mapping>
7          <filter-name>UrlRewriteFilter</filter-name>
8          <url-pattern>/*</url-pattern>
9   </filter-mapping>

继续在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>
           <name>html rule</name>
           <from>/test/thread([0-9]+)_([0-9]+).html</from>
           <to>/show.jsp?id=$1&order=$2</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>

实现当访问http://localhost/test/thread12_56.html 时,会转向http://localhost/show.jsp?id=12&order=56这个动态页面。
 
posted @ 2012-10-30 17:24  Code_maker  阅读(1720)  评论(0编辑  收藏  举报