Springboot2(3)静态资源处理

版权声明:转载请注明出处 https://blog.csdn.net/cowbin2012/article/details/85237760

默认静态资源映射

Spring Boot 默认将 /** 所有访问映射到以下目录:

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources

当文件夹有相同名字的文件时,以后面的文件为准,假如以上所有文件夹都有login.html文件,则显示/META-INF/resources下的login.html

自定义静态资源映射

第一种方式:静态资源配置类

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

第二种方式:在application.properties配置

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

访问地址:http://localhost:7091/static/login.html

注意:通过spring.mvc.static-path-pattern这种方式配置,会使Spring Boot的默认配置失效,也就是说,/public /resources 等默认配置不能使用。配置中配置了静态模式为/static/,就只能通过/static/来访问。

源码地址:https://gitee.com/cowboy2016/springboot2-open

猜你喜欢

转载自blog.csdn.net/cowbin2012/article/details/85237760