SpringBoot静态资源访问与配置原理

一、静态资源访问

1.1 静态的内容

Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.

1 静态资源目录

  • 使用根路径+静态资源名即可访问
- static
- public 
- resources/resources
- META-INF/resources

在这里插入图片描述

以上为默认静态资源访问路径

2 原理

  • 静态资源映射:/**
  • 请求先进来,先去找Controller能不能处理,如果不能就去静态资源处理器处理,如果还找不到就是404。

3 访问前缀配置

我们可以为静态资源配置访问前缀,以免我们后期进行权限控制时候影响。/

  • static-path-pattern
spring:
  mvc:
    static-path-pattern: /res/** #对于欢迎页、小图标影响

4 WebJar(了解即可)

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>
  • 配置完成后通过http:localhost:8080/jquery/3.5.1/jquery.js

1.2 欢迎页支持

  • 再静态资源下放入index文件即可
  • 可以使用静态资源路径,但不要使用前缀功能,否则是失效。
spring:
#  mvc:
#    static-path-pattern: /res/**   这个会导致welcome page功能失效

  resources:
    static-locations: [classpath:/haha/]

1.3 自定义 Favicon

  • favicon.ico 放在静态资源目录下即可。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值