背景:有一個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