利用urlrewriter实现页面伪静态

来源:互联网 发布:剑灵路飞捏脸数据图 编辑:程序博客网 时间:2024/06/05 03:07

 一个项目在后期运行的过程中,往往有各种理由去做页面的url地址的更换,给用户一种静态页面的感觉,其中利用到的技术主要有伪静态和真静态,其中真静态又根据存储位置的不同而会有两种方式,一种是定期生产html文件存储在磁盘上,而另外一种情况则是存储在内存中,在请求的时候直接从内存中取数据。

        在这主要说的是伪静态,意思就是原本的动态页面,比如jsp,action等页面,但在用户的浏览器里看到的并不是以jsp结尾的,而是自定义的方式,比如htm或shtml等等。这里利用到的就是urlrewriter,http://tuckey.org/urlrewrite/

里面有详细的使用说明。

       首先把urlrewrite-3.2.0.jar加入到项目的classpath中

       然后在web.xml中初始化。加上下面的代码

[html] view plaincopyprint?
  1. <filter>  
  2.     <filter-name>UrlRewriteFilter</filter-name>  
  3.     <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.     <filter-name>UrlRewriteFilter</filter-name>  
  7.     <url-pattern>/*</url-pattern>  
  8.     <dispatcher>REQUEST</dispatcher>  
  9.     <dispatcher>FORWARD</dispatcher>  
  10. </filter-mapping>  

        最后在WEB-INF目录下建一个urlrewrite.xml文件,内容如下:
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8" ?>   
  2.   <!DOCTYPE urlrewrite (View Source for full doctype...)>   
  3. - <!--   
  4.   
  5.     Configuration file for UrlRewriteFilter  
  6.     http://www.tuckey.org/urlrewrite/  
  7.   
  8.   
  9.   
  10.   -->   
  11. <urlrewrite use-query-string="false" use-context="false">  
  12. <rule enabled="true">  
  13.   <note>The rule means that requests to /test/status/ will be redirected to /rewrite-status the url will be rewritten.</note>   
  14.   <from casesensitive="false">/test/status/</from>   
  15.   <to type="redirect" last="false" qsappend="false">%{context-path}/rewrite-status</to>   
  16.   </rule>  
  17. <outbound-rule enabled="true" encodefirst="false">  
  18.   <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>   
  19.   <from casesensitive="false">/rewrite-status</from>   
  20.   <to type="forward" last="false" qsappend="false">/test/status/</to>   
  21.   </outbound-rule>  
  22. - <!--   
  23.   
  24.     INSTALLATION  
  25.   
  26.         in your web.xml add...  
  27.   
  28.         <filter>  
  29.             <filter-name>UrlRewriteFilter</filter-name>  
  30.             <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>  
  31.             <init-param>  
  32.                 <param-name>logLevel</param-name>  
  33.                 <param-value>WARN</param-value>  
  34.             </init-param>  
  35.         </filter>  
  36.         <filter-mapping>  
  37.             <filter-name>UrlRewriteFilter</filter-name>  
  38.             <url-pattern>/*</url-pattern>  
  39.         </filter-mapping>  
  40.   
  41.      EXAMPLES  
  42.   
  43.      Redirect one url  
  44.         <rule>  
  45.             <from>/some/old/page.html</from>  
  46.             <to type="redirect">/very/new/page.html</to>  
  47.         </rule>  
  48.   
  49.     Redirect a directory  
  50.         <rule>  
  51.             <from>/some/olddir/(.*)</from>  
  52.             <to type="redirect">/very/newdir/$1</to>  
  53.         </rule>  
  54.   
  55.     Clean a url  
  56.         <rule>  
  57.             <from>/products/([0-9]+)</from>  
  58.             <to>/products/index.jsp?product_id=$1</to>  
  59.         </rule>  
  60.     eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.  
  61.   
  62.     Browser detection  
  63.         <rule>  
  64.             <condition name="user-agent">Mozilla/[1-4]</condition>  
  65.             <from>/some/page.html</from>  
  66.             <to>/some/page-for-old-browsers.html</to>  
  67.         </rule>  
  68.     eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older  
  69.     browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.  
  70.   
  71.     Centralised browser detection  
  72.         <rule>  
  73.             <condition name="user-agent">Mozilla/[1-4]</condition>  
  74.             <set type="request" name="browser">moz</set>  
  75.         </rule>  
  76.     eg, all requests will be checked against the condition and if matched  
  77.     request.setAttribute("browser", "moz") will be called.  
  78.   
  79.       
  80.   
  81.   -->   
  82.   </urlrewrite>  

 所有的规则配置都写在这里。第一个常用个规则就是站内的简单重写。
<rule>
  <from></from>
  <to type="forward></to>
</rule>
  <from></from>写上你自己定义的访问地址,<to type="forward></to>就是实际的访问地址。比如我们实际的访问地址是:http://yousite.com/entity.htm ?category=user&page=2.而我们想把它重写为http://yousite.com/entity/uesr/page_2.html。这样看起来比我们实际的要好看的多。我们就应该这样的写:  
<rule>
  <from>^/(/w+)/(/w+)/page_(/d+)/.html$</from>
  <to type="forward">/$1.htm?category=$2&amp;page=$3</to>
 </rule>
   简单的介绍一下常用的正规表示式: 
代码 说明 
. 匹配除换行符以外的任意字符 
/w 匹配字母或数字或下划线或汉字 
/s 匹配任意的空白符 
/d 匹配数字 
/b 匹配单词的开始或结束 
^ 匹配字符串的开始 
$ 匹配字符串的结束

常用的&要用  &amp;来表示。$1,$2代表与你配置正规表达式>/(/w+)/(/w+)/相对应的参数。<to type="forward">默认的是 type="forward".
  另一个常用的规则就是连接外部的网站。就要用到。<to type="redirect">
 <rule>
     <from>^/rss/yahoo/.html$</from>
       <to type="redirect">    http://add.my.yahoo.com/rss? url=http://feed.feedsky.com/MySiteFeed
      </to>
 </rule>

0 0