码迷,mamicode.com
首页 > 其他好文 > 详细

5.对静态资源映射的规则

时间:2020-03-15 15:05:20      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:miss   private   网页   Fix   version   nconf   cond   depend   art   

对静态资源映射的映射类配置:

技术图片

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)、 webjars

所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;
webjars:以jar包的方式引入静态资源;
 

技术图片

 

访问网址进行一下测试:

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

技术图片

可以成功访问

 

ResourceProperties.java

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
  //可以设置和静态资源有关的参数,缓存时间等
.....
}

技术图片

 

在访问的时候只需要写webjars下面资源的名称即可

具体的引入maven再官网可进行查询

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

 

此时引入之后就可以使用!!!!

 

2)、存放静态资源的位置

"/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
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

技术图片

实质是一个数组的形式,该配置之后静态文件不能访问

 

 此时原来静态文件的文件

技术图片

5.对静态资源映射的规则

标签:miss   private   网页   Fix   version   nconf   cond   depend   art   

原文地址:https://www.cnblogs.com/lyh233/p/12497574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!