最近在做项目的时候遇到个问题,就是用户访问的主页是http://ip:port/projectName/folder/index.html。但是呢在项目名和主页之间就是多了那么一层路径folder,就是这么一层路径让用户感觉非常不爽,我就找到了这个非常简单的解决方案:urlwriterfilter。配置完之后当用户访问http://ip:port/projectName/index.html就是在访问http://ip:port/projectName/folder/index.html,而且地址栏还是http://ip:port/projectName/index.html。

        首先到http://urlrewritefilter.googlecode.com/files/urlrewritefilter-3.2.0.zip下载urlrewritefilter。下载完成之后将jar包拷贝到WEB-INF下的lib里,然后将urlrewrite.xml复制到项目下的WebRoot/WEB-INF/目录下。接着在web.xml里配置过滤器,如下:

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

这样urlrewrite我们就配置好了,我们就可以进行访问路径的配置了。假如我们要配置的就是我开篇所说的那样。

我们想让用户访问http://ip:port/projectName/index.html其实跳转的却是http://ip:port/projectName/folder/index.html,而浏览器的地址栏却没变仍然是http://ip:port/projectName/index.html,我们就可以在urlrewrite.xml里做如下配置:

<rule>
        <!-- 我们访问的地址 -->
        <from>/index.html</from>
        <!-- 实际访问的地址 -->
        <to>/folder/index.html</to>
</rule>


       这样我的要求就实现了。

小结:配置的时候还可以配置是转发还是重定向的属性,而且还可以对带参数的url进行伪装,在xml的配置里还可以使用正则表达式进行配置,例如:<from>^/folder/index/([0-9]+)_([a-z]+).html$</from>。

相关文章:

  • 2022-02-28
  • 2021-11-18
  • 2022-12-23
  • 2021-09-16
  • 2022-02-10
  • 2021-09-23
  • 2022-02-01
  • 2022-12-23
猜你喜欢
  • 2021-11-25
  • 2021-10-12
  • 2021-10-01
  • 2021-08-27
  • 2021-08-29
相关资源
相似解决方案