web访问路径url省略jsp等后缀--伪静态

下载urlrewritefilter-4.0.3.jar添加到项目中

在WEB-INF中新建urlrewrite.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite
    PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
    "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
    <rule>
        <from>^/index$</from>  
        <to type="forward">/index.jsp</to> 
    </rule>
    <rule>
        <from>^/login$</from>  
        <to type="forward">/login.jsp</to> 
    </rule>
    <rule>
        <from>^/register$</from>  
        <to type="forward">/register.jsp</to> 
    </rule>
    <rule>
        <from>/useraction/myprofile.html</from>
        <to type="forward">/useraction/myprofile.do</to>
    </rule>
</urlrewrite>

在web.xml中最上面添加配置:

 <!-- 加到任何servlet映射的顶部,不然可能有些路径不能被过滤到
         http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html
     -->
      <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <!-- 
            设备文件重加载间隔 (0默示随时加载, -1默示不重加载, 默认-1) 
        -->
        <init-param>
            <param-name>confReloadCheckInterval</param-name>
            <param-value>60</param-value>
        </init-param>

        <!-- 自定义配置文件的路径,是相对context的路径,(默认位置 /WEB-INF/urlrewrite.xml) -->
        <init-param>
            <param-name>confPath</param-name>
<!--             <param-value>classpath:spring/urlrewrite.xml</param-value> -->
             <param-value>/WEB-INF/urlrewrite.xml</param-value>
        </init-param>

        <!-- 
            设置日志级别(将被记录到日志中)
               可以为: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, commons, slf4j,
               比如 sysout:DEBUG(设置到控制台调试输出级别) 
            (默认级别 WARN) -->
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>DEBUG</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

然后访问 http://127.0.0.1:8080/springtest/login 正确跳转至登录页面

posted @ 2020-12-23 14:57  旧吉他再弹不出浪漫  阅读(183)  评论(0编辑  收藏  举报