Springboot-2.1 系列 - 3、静态资源处理

版权声明:本文转载自 https://blog.csdn.net/cowbin2012/article/details/85237760
(部分修改补充)

默认静态资源映射

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

classpath:/public
classpath:/static
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://github.com/c0Alan/Repo-IDEA/tree/master/springboot-demo/springboot-2-1-x

项目说明

static-resources-s1:8080

依次访问:
http://ip:8080/public.html

http://ip:8080/static.html

http://ip:8080/resources.html

http://ip:8080/NETA-INF.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值