springboot 静态资源目录、index.html首页和ico图标

概述

web开发,肯定有静态资源(html、css、js、图片),但是 springboot工程目录 与 传统java web工程目录不同,没有webapps文件夹

静态资源目录

springboot存放静态资源路径如下:

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/   ---(推荐)
classpath:/public/

classpath:指maven工程的java文件夹 和 resources文件夹

springboot框架源码实现

在类中有对静态资源路径的配置:

public class ResourceProperties {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

修改静态资源目录

# 静态资源映射路径
spring.resources.static-locations = classpath:/html/,classpath:/css/

注意: 修改后,原有的静态资源目录 全部失效

springboot框架源码

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {

添加静态资源目录

方式一

原有的静态资源目录 和 新增的目录,写在一起

spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/html/,classpath:/css/

方式二

通过配置类实现

https://www.malaoshi.top/show_1IXf6OCkoHc.html

添加 外部 静态资源目录

使用 file:目录 方式添加

例子:

spring.resources.static-locations=file:D:/lvyou/

静态资源映射

静态资源url

默认的静态资源url,如下:

spring.mvc.static-path-pattern=/**

/** 表示 http://ip:port/context/ 路径下的资源,以及 子路径 下的资源

映射

访问 http://localhost:8080/1.jpg,springboot就会到下面这些静态资源目录中去找 1.jpg

classpath:/META-INF/resources/1.jpg
classpath:/resources/1.jpg
classpath:/static/1.jpg
classpath:/public/1.jpg

修改静态资源url

spring.mvc.static-path-pattern=/abc/**

注意: 修改后,原有配置 /** 失效

访问 http://localhost:8080/abc/1.jpg,springboot就会到下面这些静态资源目录中去找 1.jpg

classpath:/META-INF/resources/1.jpg
classpath:/resources/1.jpg
classpath:/static/1.jpg
classpath:/public/1.jpg

增加静态资源url

通过配置类实现

https://www.malaoshi.top/show_1IXf6OCkoHc.html

首页

index.html

在上面的静态目录下

springboot框架源码实现

public class WebMvcAutoConfiguration {
            private Optional<Resource> getWelcomePage() {
            String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
            return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
        }

        private Resource getIndexHtml(String location) {
            return this.resourceLoader.getResource(location + "index.html");
        }
}

.ico图标

/favicon.ico

SpringBoot 默认是开启Favicon,并且提供了一个默认的Favicon,如果想关闭Favicon,只需要在application.properties中添加

spring.mvc.favicon.enabled=false

springboot框架源码实现

@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;
    }

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

    @Bean
    public SimpleUrlHandlerMapping faviconHandlerMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
        return mapping;
    }

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

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

}

原文出处:/show_1EF5rFJgBLua.html