静的リソースに対するSpringBootのマッピングルール
- デフォルトの静的リソースパスは、このソースコードで構成されています
@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/" };
- このコードは、外部からインポートされた静的リソースファイル構造を構成します
@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サイトを入力してください
- 「/ **」の表現:現在のプロジェクトの任意のリソースにアクセスします
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
クラスパスは次の図のリソースであり、このディレクトリにMETA-INF / resourcesを作成することでアクセスできます。
localhost:8080 / abc ===静的リソースに移動してabcを検索します
- ウェルカムページ:静的リソースフォルダーの下にあるすべてのindex.htmlページ。
インデックスページを見つけるために「/ **」によってlocalhost:8080 /にマップされます。
- 静的リソースの場所を構成する
spring.resources.static-locations=classpath:/suitianshuang/