使用urlrewrite 实现URL重写

1、下载urlrewrite-2.6.0.jar 放到所在项目 WEB-INF/lib下

2、把文件 urlrewrite.xml 放到WEB-INF目录下

     urlrewrite.xml内容如下:

 

     <?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">  
    <!-- %{context-path}  
    type : forward | redirect  
 
 -->  
<urlrewrite>   
    <rule>  
        <from>/test_(.*)_(.*)</from>  
        <to type="forward">/test.action?aatest=$1&amp;aatest1=$2</to>  
    </rule>  
</urlrewrite> 

 3、配置 web.xml

          <!-- url重写-->
   <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>  

 <!-- 配置struts2 start -->
 <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>*.action</url-pattern>
  
  <dispatcher>REQUEST </dispatcher> 
      <dispatcher>FORWARD </dispatcher> 
      <dispatcher>INCLUDE </dispatcher>
 </filter-mapping>
 <!-- 配置struts2 end -->

 

 

这样就完成的url的重写,原来要访问的action是这样的  http://localhost:8080/goods/test.action?aatest=值1&aatest2=值2

重写后,变成了 http://localhost:8080/goods/test_值1_值2   ,这样看起来好看多了吧。

 

 

-------------------------------------------------------------------------------------------------------------------------------------

常用rule

 

 

 <!--
    <rule>  
       
        <from>/test_(.*)_(.*)</from>  
        <to type="forward">/test.action?aatest=$1&amp;aatest1=$2</to>  
    </rule>  
    test.action?aatest=值1&aatest1=值2
       重写后  test_值1_值2
      -->
      ------------------------------------------------------------------------
      <rule>
       <from>^/test/(.*)$</from>
       <to>/test.action?aatest=$1</to>
       <!--
        test.action?aatest=参数1
         重写后 test/参数1
        -->
      </rule>

 

  ------------------------------------------------------------------------

原来 test.action?aatest=参数             test.action?aatest=0005

重写后  test/参数.html                          test/0005.html

  <rule>  
        <from>^/test/(.*).html</from>  
        <to type="forward">/test.action?aatest=$1</to>  
    </rule>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值