urlrewrite可以将动态的URL进行伪静态

urlrewrite可以将动态的URL进行伪静态,比如把
xxxx.jsp?id=1转为http://xxxx/xxx/1
这里发现一个好工具:tuckey urlrewrite,使用方法如下:

1 下载urlrewrite,官方下载地址:http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip

2 放到项目的LIB包中
3 建立urlrewrite.xml文件,放到WEB-INF
目录下,编写如下:
  <?xml version="1.0" encoding="utf-8"?>    
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"    
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">    
  
<!--    
  
Configuration file for UrlRewriteFilter    
http://tuckey.org/urlrewrite/    
  
-->    
<urlrewrite>    
    <rule>    
        <from>^/([a-z]+)$</from>    
        <to type= "forward" >/world.jsp?id=$1</to>    
    </rule>    
    <rule>  
        <from>^/world/(.*)$</from>  
        <to>/world.jsp?tid=$1</to>  
    </rule>  
    <rule>  
        <from>^/(.*).html$</from>  
        <to>/test1/$1.jsp</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>  
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">

<!--

Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/

-->
<urlrewrite>
<rule>
<from>^/([a-z]+)$</from>
<to type= "forward" >/world.jsp?id=$1</to>
</rule>
<rule>
    <from>^/world/(.*)$</from>
    <to>/world.jsp?tid=$1</to>
</rule>
<rule>
<from>^/(.*).html$</from>
<to>/test1/$1.jsp</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>


  rule是url重写规则,from是显示出来的地址,to是映射的实际地址,$1是重写参数,可以为多个,()里是匹配的正则表达式.
  比如:
  <rule>  
        <from>/(.*)/login.htm</from>  
        <to type="forward">/yui/login.htm?name=$1</to>  
    </rule>

  http://localhost:8080/app/abc/login.htm其实际请求的url为http://localhost:8080/app/yui/login.htm?name=abc

4 配置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>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/urlrewrite.xml </param-value>
</context-param>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值