<mvc:default-servlet-handler/> 這個Spring MVC xml文件的屬性,主要是處理web項目的靜態文件問題。
每次請求過來,先經過 DefaultServletHttpRequestHandler 判斷是否是靜態文件,如果是靜態文件,則進行處理,不是則放行交由 DispatcherServlet 控制器處理。
/* *開啟對靜態資源的訪問 * 類似在Spring MVC的配置文件中設置<mvc:default-servlet-handler/>元素 */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }
可以通過自定義一個WebMvcConfigurer類型的bean,改寫configureDefaultServletHandling 方法,如下。
@Configuration public class MyWebConfigurer extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
這樣就設置了一個默認的servlet,在加載靜態資源的時候就會按照servlet方式去加載了。
Spring MVC
中<mvc:default-servlet-handler />
的需求是什么?
使用此 handler
調度程序會將所有請求轉發到默認的Servlet
.要啟用該功能,您可以使用注釋或基於xml的配置.
======================================
What is the DefaultServlet
The default servlet is the servlet which serves static resources as well as serves the directory listings (if directory listings are enabled).
public class DefaultServletHandlerConfigurer extends Object
Configures a request handler for serving static resources by forwarding the request to the Servlet container's "default" Servlet. This is intended to be used when the Spring MVC DispatcherServlet is mapped to "/" thus overriding the Servlet container's default handling of static resources.
Since this handler is configured at the lowest precedence, effectively it allows all other handler mappings to handle the request, and if none of them do, this handler can forward it to the "default" Servlet.
REF
https://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html#what
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurer.html
REF
https://blog.csdn.net/zhengyangzkr/article/details/70174296
https://vimsky.com/zh-tw/examples/detail/java-method-org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.enable.html
https://www.cnblogs.com/hujunzheng/p/9682960.html
======================================
下面完整示例來自 https://geek-docs.com/spring/spring-tutorials/defaultservlet.html
DefaultServlet
DefaultServlet 是大多數 Web 應用的默認資源服務 Servlet,用於提供 HTML 頁面和圖像等靜態資源。
DefaultServletHttpRequestHandler 嘗試在啟動時自動檢測容器的默認 Servlet,例如 Tomcat,Jetty,Wildfly 和 Resin。 如果默認 Servlet 是使用其他名稱自定義配置的,則必須明確提供默認 Servlet 的名稱。
如果我們重寫DefaultServlet's路由(/),則可以使用 DefaultServletHandlerConfigurer's enable() 方法啟用它,以便我們仍然可以使用容器的默認 Servlet 提供靜態資源。
Spring DefaultServlet示例
在以下應用中,我們將 Spring 調度程序 servlet 配置為 / 路徑,該路徑將重寫默認 servlet 的路徑。 我們使用 DefaultServletHandlerConfigurer 啟用默認 servlet。
該應用提供一個簡單的 HTML 主頁,這是一個靜態資源。
這是項目結構。
pom.xml
src
├───main
│ ├───java
│ │ └───com
│ │ └───zetcode
│ │ └───config
│ │ MyWebInitializer.java
│ │ WebConfig.java
│ ├───resources
│ │ logback.xml
│ └───webapp
│ index.html
└───test
└───java
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>defaultservletex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.3.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> </plugins> </build> </project>
這是 Maven 構建文件。 我們具有以下依賴性:用於 Java Servlet 技術的 javax.servlet-api,用於日志記錄的 logback-classic 和用於創建 Spring Web MVC 應用的 spring-webmvc。
maven-war-plugin 創建 Web 存檔(WAR)。
com/zetcode/config/MyWebInitializer.java
package com.zetcode.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
MyWebInitializer 初始化 Spring Web 應用。
@Override protected String[] getServletMappings() { return new String[]{"/"}; }
我們將 Spring DispatcherServlet 注冊到 / 路徑。 這代替了 DefaultServlet ; 因此,我們必須在配置文件中注冊一個默認的 servlet 處理程序。
com/zetcode/config/WebConfig.java
package com.zetcode.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
WebConfig 通過 @EnableWebMvc 啟用 Spring MVC,並通過 DefaultServletHandlerConfigurer's enable() 方法配置 DefaultServlet。
@Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }
configureDefaultServletHandling()使用 URL 映射 /** 和相對於其他 URL 映射的最低優先級來配置 DefaultServletHttpRequestHandler。 這樣,靜態資源請求由容器的默認 Servlet 處理。
webapp/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home page</title> </head> <body> <p> This is home page. </p> </body> </html>
這是主頁。 它是靜態資源,由 DefaultServlet 自動提供服務。
======================================
Spring MVC ServletMappings 路徑匹配原理(截取匹配)
通常情況下,大家配置 spring mvc的路徑攔截(ServletMapping)都習慣配置 “/”,但在某些特殊場景下,你可能希望所有的mvc的路徑都有個固定的前綴,形如“/springmvc/*”,如果你在springmvc的ServletMapping中配置了這種路徑,那么你的URL訪問地址和controller的path就要注意了:如果你的地址欄URL為"/springmvc/user",那么你的controller中的path就必須配置"/user",因為spring mvc攔截了所有以“/springmvc”為前綴的請求,而在匹配的時候也截掉了“/springmvc”,用剩下的“/user”匹配controller。
https://blog.csdn.net/renchenglin118/article/details/93207031
======================================