url-pattern*.htmlurl-pattern导致根路径访问404的问题

背景:有一个spring mvc项目,web.xml的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">


    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!--<param-value>classpath:META-INF/spring/spring-config.xml,classpath:META-INF/spring/spring-shiro.xml</param-value>-->
        <param-value>classpath:META-INF/spring/spring-config.xml</param-value>
    </context-param>

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>manage-web.all.root</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:logback.xml</param-value>
    </context-param>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:META-INF/spring/springMVC-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

   

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 设置超时时间-->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <error-page>
        <error-code>400</error-code>
        <location>/static/error.html</location>
    </error-page>
    <!-- 404 页面不存在错误 -->
    <error-page>
        <error-code>404</error-code>
        <location>/static/error.html</location>
    </error-page>
    <!-- 500 服务器内部错误 -->
    <error-page>
        <error-code>500</error-code>
        <location>/static/error.html</location>
    </error-page>

    <error-page>
        <error-code>401</error-code>
        <location>/static/error_authorization.html</location>
    </error-page>

</web-app>

有一个controller是这样的定义的

    @RequestMapping(value = {"/manage/main", "/"}, method = RequestMethod.GET)
    public ModelAndView index2() throws ServletException {
}

通过localhost:8080/可以访问。

为了能够过滤html静态文件,加了

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

之后再运行项目,访问localhost:8080/,就报错404了。

结论:

参考https://blog.csdn.net/shuair/article/details/86645707

请求根路径(ip:port/项目名/,本项目部署再ROOT文件夹下面,不需要项目名) 时,如果我们自定义的servlet没有配置“/*”前缀匹配,那么请求路径无法依据精确匹配、前缀匹配及扩展名匹配被任何servlet匹配到,此时会增加一个资源文件(welcomeResources)匹配方式,先拼接welcome-file的值再进行如上三种匹配。

本项目种一开始没有配置<url-pattern>*.html</url-pattern>静态资源,也没有配置“/*”前缀匹配,在访问localhost:8080/时,请求路径无法依据精确匹配、前缀匹配及扩展名匹配被任何servlet匹配到,此时welcome-file的值再进行如上三种匹配,仍然没有匹配到,进入<url-pattern>/</url-pattern>这个servlet执行。

后来增加了配置<url-pattern>*.html</url-pattern>静态资源,访问localhost:8080/时,请求路径无法依据精确匹配、前缀匹配及扩展名匹配被任何servlet匹配到,此时welcome-file的值再进行如上三种匹配,比如说localhost:8080/index.html这个时候就后被<url-pattern>*.html</url-pattern>匹配到了。由于本项目种没有index.html,因此报错404

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章