1. 程式人生 > 其它 >12.springboot與web開發

12.springboot與web開發

springboot對靜態資源的對映規則:
@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {
    可以設定和靜態資源有關的引數(快取時間等)--->和下面的:Duration cachePeriod = this.resourceProperties.getCache().getPeriod();對應起來!
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        if (!registry.hasMappingForPattern("/webjars/**")) {
            //所有的/webjars/**,都去classpath:META-INF/resources/webjar找資源
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"})
            .addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"})
            .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if (!registry.hasMappingForPattern(staticPathPattern)) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

    }
}

1.所有的/webjars/**(所有請求),都去classpath:META-INF/resources/webjar找資源

webjars:以jar包的形式去引入靜態資源 參看地址;https://www.webjars.org/ jquery的webjar目錄結構如下:和程式碼中的對應上了
訪問路徑:localhost:8080/webjars/jquery/3.5.1/jquery.js可以訪問到jquery的webjar中的內容!
<!--引入jquery的webjar-->  在訪問時只需要寫webjars下面的資源的名稱即可!
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>

2."/**"訪問當前專案的任何資源:(靜態資源的儲存資料夾)

分析原始碼得知:當訪問當前專案的任何資源時:會去當前類路徑下的以下資料夾進行查詢:
"classpath:/META-INF/resources/", 
"classpath:/resources/", 
"classpath:/static/", 
"classpath:/public/"
"/":當前專案的根路徑

當訪問路徑是:localhost:8080/abc=====>去靜態資原始檔夾中找abc
以下路徑可以存放自定義靜態資源!

3.歡迎頁的對映

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
      FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
   WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
         new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
         this.mvcProperties.getStaticPathPattern());
   welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
   welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
   return welcomePageHandlerMapping;
}

歡迎頁:靜態資原始檔夾下的所有index.html頁面;被"/**"對映
當訪問路徑是:localhost:8080/  會去靜態檔案資源夾中找index.html