静的リソースのSpringBootマッピングルール、WebMvcAutoConfigurationクラスのソースコード分析

静的リソースに対するSpringBootのマッピングルール


  1. デフォルトの静的リソースパスは、このソースコードで構成されています
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
    
    

	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
    
     "classpath:/META-INF/resources/",
			"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
  1. このコードは、外部からインポートされた静的リソースファイル構造を構成します
@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
			if (!this.resourceProperties.isAddMappings()) {
    
    
				logger.debug("Default resource handling disabled");
				return;
			}
			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
			CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
			if (!registry.hasMappingForPattern("/webjars/**")) {
    
    
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			if (!registry.hasMappingForPattern(staticPathPattern)) {
    
    
				customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
						.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
		}

webjarsウェブサイト
https://www.webjars.org/
はjQueryの依存関係を導入しています

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>

ここに画像の説明を挿入
このディレクトリは、ソースコードのマッピングパスと一致しています

http:// localhost:8090 / webjars / jquery / 3.5.1 / jquery.js
アクセスするには、ブラウザのアドレスバーに上記のWebサイトを入力してください

  1. 「/ **」の表現:現在のプロジェクトの任意のリソースにアクセスします
"classpath:/META-INF/resources/",
"classpath:/resources/", 
"classpath:/static/", 
"classpath:/public/"

クラスパスは次の図のリソースであり、このディレクトリにMETA-INF / resourcesを作成することでアクセスできます。
ここに画像の説明を挿入

localhost:8080 / abc ===静的リソースに移動してabcを検索します

  1. ウェルカムページ:静的リソースフォルダーの下にあるすべてのindex.htmlページ。
    インデックスページを見つけるために「/ **」によってlocalhost:8080 /にマップされます。

ここに画像の説明を挿入

  1. 静的リソースの場所を構成する
spring.resources.static-locations=classpath:/suitianshuang/

おすすめ

転載: blog.csdn.net/weixin_43941676/article/details/108612578