首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

urlrewrite 配备

2012-12-26 
urlrewrite 配置在进行链接伪静态时用到了urlrewrite,一个开源的jar包,配置使用:首先下载jar包导入,下载地

urlrewrite 配置

    在进行链接伪静态时用到了urlrewrite,一个开源的jar包,配置使用:

首先下载jar包导入,下载地址:http://code.google.com/p/urlrewritefilter/downloads/list ,
在web.xml 加入UrlRewriteFilter

<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>


  可选的初始化参数 :
  confReloadCheckInterval:间隔冲洗加载时间秒。调试时用这个。
  confPath :制定配置文件,不加默认(/WEB-INF/urlrewrite.xml)
  statusPath:查看urlrewrite配置的详细,默认/rewrite-status。若在  <url-pattern>/a/*</url-pattern> 则是:/a/rewrite-status
  statusEnabled: 若为false,则 statuspath无效,默认true
  statusEnabledOnHosts,allowConfSwapViaHttp,modRewriteConf,modRewriteConfText

  然后urlrewrite.xml 中配置,下面这个是jar包中自带的配置和说明:
 
  <?xml version="1.0" encoding="utf-8"?><!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"        "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"><!--    Configuration file for UrlRewriteFilter    http://tuckey.org/urlrewrite/--><urlrewrite>  <rule>            <from>/some/old/page.html</from>            <to type="redirect">/very/new/page.html</to>        </rule>   <!-- Redirect a directory-->        <rule>            <from>/some/olddir/(.*)</from>            <to type="redirect">/very/newdir/$1</to>        </rule>    <!-- Clean a url-->        <rule>    <name>简化url</name>    <note>/products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.</note>            <from>/products/([0-9]+)</from>            <to>/products/index.jsp?product_id=$1</to>        </rule>        <rule>    <note>will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older    browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4</note>            <condition name="user-agent">Mozilla/[1-4]</condition>            <from>/some/page.html</from>            <to>/some/page-for-old-browsers.html</to>        </rule>   <!--   Centralised browser detection -->        <rule>            <condition name="user-agent">Mozilla/[1-4]</condition>            <set type="request" name="browser">moz</set>        </rule></urlrewrite>


其中rule 还可以这样
   <rule>        <name>test Rule </name>        <note>this is a  test normalRule.</note>        <condition type="local-port">8080</condition>        <condition type="dayofweek">1</condition>        <condition type="ampm" operator="less">12</condition>        <condition type="year" operator="greaterorequal">2010</condition>        <run method="justRun" jsonhandler="true"/>         <from>/some/olddir/(.*)</from>         <to type="redirect">/very/newdir/$1</to>    </rule>


condition 所支持的type类型,可以在:TypeConverter类中有:
 public String getType() {        switch (type) {            case TYPE_TIME:                return "time";            case TYPE_TIME_YEAR:                return "year";            case TYPE_TIME_MONTH:                return "month";            case TYPE_TIME_DAY_OF_MONTH:                return "dayofmonth";            case TYPE_TIME_DAY_OF_WEEK:                return "dayofweek";            case TYPE_TIME_AMPM:                return "ampm";            case TYPE_TIME_HOUR_OF_DAY:                return "hourofday";            case TYPE_TIME_MINUTE:                return "minute";            case TYPE_TIME_SECOND:                return "second";            case TYPE_TIME_MILLISECOND:                return "millisecond";            case TYPE_ATTRIBUTE:                return "attribute";            case TYPE_AUTH_TYPE:                return "auth-type";            case TYPE_CHARACTER_ENCODING:                return "character-encoding";            case TYPE_CONTENT_LENGTH:                return "content-length";            case TYPE_CONTENT_TYPE:                return "content-type";            case TYPE_CONTEXT_PATH:                return "context-path";            case TYPE_COOKIE:                return "cookie";            case TYPE_HEADER:                return "header";            case TYPE_LOCAL_PORT:                return "local-port";            case TYPE_METHOD:                return "method";            case TYPE_PARAMETER:                return "parameter";            case TYPE_PATH_INFO:                return "path-info";            case TYPE_PATH_TRANSLATED:                return "path-translated";            case TYPE_PROTOCOL:                return "protocol";            case TYPE_QUERY_STRING:                return "query-string";            case TYPE_REMOTE_ADDR:                return "remote-addr";            case TYPE_REMOTE_HOST:                return "remote-host";            case TYPE_REMOTE_USER:                return "remote-user";            case TYPE_REQUESTED_SESSION_ID:                return "requested-session-id";            case TYPE_REQUEST_URI:                return "request-uri";            case TYPE_REQUEST_URL:                return "request-url";            case TYPE_SESSION_ATTRIBUTE:                return "session-attribute";            case TYPE_SESSION_IS_NEW:                return "session-isnew";            case TYPE_SERVER_PORT:                return "port";            case TYPE_SERVER_NAME:                return "server-name";            case TYPE_SCHEME:                return "scheme";            case TYPE_USER_IN_ROLE:                return "user-in-role";            case TYPE_EXCEPTION:                return "exception";            default:                return "";        }    }


<run/> 对应的method中的参数必须是一下几种。
{ServletRequest.class, ServletResponse.class},
{ServletRequest.class},
{ServletResponse.class},
{HttpServletRequest.class, HttpServletResponse.class},
{HttpServletRequest.class},
{HttpServletResponse.class}

在类Run中用到反射来验证方法是否存在,反射时默认的参数是以上几种。
在过滤是符合规则 会执行run中对应的方法然后在转向真正的url。

好了,ok。

热点排行