UrlreWirte的使用

来源:互联网 发布:hadoop spark java 编辑:程序博客网 时间:2024/06/09 16:43

一、首先说一下UrlreWirte的用处

1.满足搜索引擎的要求。

2.隐藏技术的实现,提高网站的移植性。

二、关于“生成静态页与UrlreWrite重写的区别”请参考网址http://xiongzaiqiren.blog.163.com/blog/static/1292871852011446021196

三、UrlreWrite在项目中的使用
1.先导入urlrewrite.jar包,jar可以自己下载或者通过maven来管理
2.在项目WEB-INF目录下新建urlrewrite.xml文件,里面配置要跳转的代码,例如:

<?xml version="1.0" encoding="utf-8"?>   
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"    
   "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">   

<urlrewrite>   
   <rule>   
             <from>/zt/ly/index.html</from>   
             <to>/front/topic/topic-showly.action?topicid=402b1ab9820002</to>   
       </rule> 

  <rule>   
     <from>/zt/ly/content/([0-9A-Za-z]+).html</from>   
            <to>/front/topic/topicalcontent1-list.action?topicid=$1</to>   
      </rule>   

    </urlrewrite>
   <!-- $1表明上面的传入的参数 具体怎么配置,我在下面说明-->

3.web.xml的配置

   <!-- UrlRewriteFilter filter -->
<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>

      <!--以上我只介绍了urlrewrite的配置,struts2的配置或者其他配置省略,其中UrlRewriteFilter filter的配置一定要在struts2的上面

   并且Struts2的配置中要加<dispatcher>FORWARD</dispatcher>这句-->

4.具体配置解释及应用

(1)不带参数,直接a标签连接即可 

urlrewrite.xml文件中
<rule><!-- 例如首页 -->                  
    <from>/zrar/infos/pa/index.html</from> 
           <to>/front/zrar/infoservice/projectapplication/index.action</to>   
</rule> 

说明:<from></from>中的url指的是你将要使用的url,<to></to>中的url是指原始的url或者一个action地址
            本来请求的action是如此的长,在浏览器里算是不美观吧,通过以上配置之后只要请求<from>中的url即相当于请求<to>中的action
jsp文件中这样编写: <a href=“${ctx}/zrar/infos/pa/index.html>中软安人</a> 即可。

(2)带一个参数的 

<rule><!--文章页-->
   <from>/zrar/infos/pa/content/([0-9A-Za-z]+).html</from>     
    <to>/front/zrar/infoservice/projectapplication/proContent-info.action?contentId=$1</to>   
</rule> 

说明:([0-9A-Za-z]+)是指穿过来的传过,这里用正则匹配了,<to>中通过$1在使用,1表示第一个,多个的会在下面讲到
jsp文件中这样编写: <a href=“${ctx}/zrar/infos/pa/content/<s:property value='id'/>.html>项目申报成功</a>

(3)带两个参数或者多个的,以下按2个参数讲解

<rule>  <!--首页最新可申报项目申报的市级链接-->
   <from>/front/zrar/pa/areaCode=([0-9A-Za-z]+)&proviceCode=([0-9A-Za-z]+).html</from>   
   <to>/front/zrar/partproject/ahead-project.action?procontentBean.areaCode=$1amp;         
       procontentBean.proviceCode=$2</to>   
</rule>

说明:以上需要传递两个参数过来,均以([0-9A-Za-z]+)表示,<to>中通过$1$2等引用使用,1表示第一个,一次类推。
     特别说明的是'&'符号需要用‘&’这样表示,直接用&是不正确的,但是网上有些文章没说明
jsp文件中这样编写: <a href=“${ctx}/zrar/pa/areaCode=<s:property  value="provinceCityId"/>&proviceCode=<s:property value="provinceCityParentId"/>.html>   
                                   <s:property value="provinceCityParentCode"/>*<s:property value="provinceCityCode"/></a>
红色的表示省市:浙江省*杭州市

5.至此urlrewrite介绍完毕,相信在不想生成过多静态页面的小网站可以值得一试,不用每次去生成静态页面,使用也比较方便。



附件下载: http://download.csdn.net/detail/shiyuezhong/4548416


原创粉丝点击