zoukankan      html  css  js  c++  java
  • 项目搭建系列之四:SpringMVC框架下使用UrlRewrite实现地址重写

      简单记录一下UrlRewrite实现地址重写功能。

    1、pom.xml

      在pom.xml增加配置UrlRewrite jar

    <!-- URL Rewrite -->
    <dependency>
        <groupId>org.tuckey</groupId>
        <artifactId>urlrewritefilter</artifactId>
        <version>4.0.4</version>
    </dependency>

    2、web.xml

      在web.xml文件中配置UrlRewriteFilter,这里可能要注意filter的加载顺序。

      <!-- 开启URLREWRITE监听 -->
      <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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
      </filter-mapping>

    3、urlrewrite.xml

      urlrewrite.xml 这个配置文件目前只能放在 WEB-INF 下。

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "\urlrewrite3.2.dtd">
      
    <urlrewrite>
      
      <rule>
        <note>重写index.jsp成index.html</note>
        <note>example:/index.html</note>
        <from>/index.html</from>
        <to type="forward">/index.jsp</to>
      </rule>
      
      <rule>
        <note>将view根目录下所有jsp资源重写成/xxx.action</note>
        <note>example:/index.action</note>
        <from>/([A-Za-z0-9]+).action</from>
        <to type="forward">/view/$1.jsp</to>
      </rule>
      
      <rule>
        <note>forward(转发模式)传参</note>
        <note>example:/user/param/fancy/8080.do</note>
        <from>/user/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+).do</from>
        <to type="forward">/view/parameter/$1.jsp?username=$2&amp;password=$3</to>
      </rule>
      
      <rule>
        <note>redirect(重定向模式)传参,to中写绝对地址</note>
        <note>example:/admin/param/fancy/8080.do</note>
        <from>/admin/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+).do</from>
        <to type="redirect">/urlrewrite-maven-example/view/parameter/$1.jsp?username=$2&amp;password=$3</to>
      </rule>
      
    </urlrewrite>

    4、注释

    第一个rule:访问 /index.html,实际上是访问了 /index.jsp

    第二个rule:访问 /xx.action , 实际上是访问了 view 目录下的 xx.jsp

    第三个rule:访问 /user/xx/yy/zz.do , 实际上是访问了 view 目录下的 parameter 目录下的 xx.jsp?username=yy&password=zz

    第四个rule:跟第三个其实是一样的,但是由于是重定向模式,所以地址栏就会显示真的是地址

    <rule>:自定义匹配规则

    <note>:注释,解释标签

    <from>:定义具体的匹配规则

    <to>:匹配成功后的目标地址

    <to type="">:type的值有两个,一个是 forward (转发,参数不丢失),一个是 redirect (重定向,地址栏显示的地址就是目标真实地址)

    $1:匹配中的第一个正则表达式的字符串的值,$2,$3,$4....也是如此

    &amp; 是 & 的实体名,代表的就是 &

  • 相关阅读:
    性能调优之网络速度检测
    通过脚本自动下载Esri会议材料
    移动目标在三维GIS中的实现方法
    java生成CSV文件
    java 提取(解压)zip文件中特定后缀的文件并保存到指定目录
    java 提取(解压)rar文件中特定后缀的文件并保存到指定目录
    spring boot 使用 POI 读取Excel文件
    spring boot jpa 使用<S extends T> List<S> findAll(Example<S> example)查询数据
    一个MySQL中两表联合update的例子(并带有group by分组)
    Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)
  • 原文地址:https://www.cnblogs.com/hehaiyang/p/4644194.html
Copyright © 2011-2022 走看看