5.對靜態資源映射的規則

對靜態資源映射的映射類配置:html

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/**")) { 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(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

    }
}

 

 

1)、 webjarsjava

全部 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找資源;
webjars:以jar包的方式引入靜態資源;
 

 

訪問網址進行一下測試:jquery

http://localhost:8088/webjars/jquery/3.3.1/jquery.jsweb

能夠成功訪問spring

 

ResourceProperties.java數組

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
  //能夠設置和靜態資源有關的參數,緩存時間等
.....
}

 

在訪問的時候只須要寫webjars下面資源的名稱便可緩存

具體的引入maven再官網可進行查詢mvc

<!-- jquery的webjar-->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1</version>
</dependency>

 

此時引入以後就可使用!!!!app

 

2)、存放靜態資源的位置maven

"/**" 訪問當前項目的任何資源,都去( 靜態資源的文件夾)找映射
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"
classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};
"/"   當前項目根路徑

 

在resources目錄下的:
-resources
-static
-public
-/

 

測試訪問hello.html網頁

能夠直接找到網頁文件的位置 

 

 3)、配置歡迎頁

 都在靜態資源文件夾下....

@Configuration
@ConditionalOnProperty(
    value = {"spring.mvc.favicon.enabled"},
    matchIfMissing = true
)
public static class FaviconConfiguration implements ResourceLoaderAware {
    private final ResourceProperties resourceProperties;
    private ResourceLoader resourceLoader;

    public FaviconConfiguration(ResourceProperties resourceProperties) {
        this.resourceProperties = resourceProperties;
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Bean
    public SimpleUrlHandlerMapping faviconHandlerMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(-2147483647);
        mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
        return mapping;
    }

    @Bean
    public ResourceHttpRequestHandler faviconRequestHandler() {
        ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
        requestHandler.setLocations(this.resolveFaviconLocations());
        return requestHandler;
    }

    private List<Resource> resolveFaviconLocations() {
        String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.
  getResourceLocations(this.resourceProperties.getStaticLocations());
List
<Resource> locations = new ArrayList(staticLocations.length + 1); Stream var10000 = Arrays.stream(staticLocations); ResourceLoader var10001 = this.resourceLoader; this.resourceLoader.getClass(); var10000.map(var10001::getResource).forEach(locations::add); locations.add(new ClassPathResource("/")); return Collections.unmodifiableList(locations); } }

全部的 **/favicon.ico 都是在靜態資源文件下找;

 與以前的進行對比

 

5)、配置文件的方式

spring.resources.static-locations=classpath:/hello,classpath:/qwe

實質是一個數組的形式,該配置以後靜態文件不能訪問

 

 此時原來靜態文件的文件

相關文章
相關標籤/搜索