二.一 继承 WebMvcConfigurerAdapter 接口进行配置
在 controller 同级目录下 创建 config 包,config 包下创建 MvcConfig 类
二.一.一 全局配置
package top.yueshushu.learn.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * @ClassName:MvcConfig * @Description TODO * @Author zk_yjl * @Date 2021/6/29 16:31 * @Version 1.0 * @Since 1.0 **/ @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { /** * 配置静态的资源信息 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/**" //路径是 /** ).addResourceLocations( "classpath:/templates/" //对应的目录是 templates目录 ); } }
resourceHandler 表示的是请求的路径, resourceLocations 表示的是该请求路径对应的资源目录信息, 是一一对应的,
可以同时注册多个。
重新启动项目:
输入路径: http://localhost:8081/Static/index.html
发现,可以访问到 templates/index.html 文件内容了。
此时,注意,以前的 SpringBoot提供的默认的四种目录无法访问到
二.一.二 指定路径配置
访问 index.html 时,没有使用任何路径前缀,这是因为 addResourceHandler 使用的是 /**
也可以进行指定
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /* registry.addResourceHandler( "/**" ).addResourceLocations( "classpath:/templates/" );*/ registry.addResourceHandler( "/yjl/**" //对应路径的路径是 yjl/ ).addResourceLocations( "classpath:/templates/" //目录是 templates ); }
前面添加了 /yjl/ 目录
直接输入
会发现,报 404
需要添加 /yjl 路径前缀才可以
注意此时:
访问原先的SpringBoot提供的那四种默认目录下的文件,是可以访问的。
addResourceHandler 的值 已经不是 /** 了,就不会替换默认的了,当配置了 /**时,就会将SpringBoot默认提供的替换。
二.一.三 配置多个路径
再新创建两个目录 s1, s2
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /* registry.addResourceHandler( "/**" ).addResourceLocations( "classpath:/templates/" );*/ registry.addResourceHandler( "/yjl/**" //对应路径的路径是 yjl/ ).addResourceLocations( "classpath:/templates/" //目录是 templates ); registry.addResourceHandler( "s1/**" //目录s1 ).addResourceLocations( "classpath:/s1/" ); registry.addResourceHandler("s2/**") //目录s2 .addResourceLocations("classpath:/s2/"); }
进行访问:
通常采用的是这一种方式
二.二 配置文件进行配置资源信息
先在 MvcConfig 类中,去掉 @Configuration 注解。
在 application.yml 配置文件的 spring目录下,添加配置信息(只放置spring目录相关的配置信息)
# 引入 数据库的相关配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true username: root password: abc123 type: com.alibaba.druid.pool.DruidDataSource #配置资源信息 mvc: static-path-pattern: /yjl/** #配置信息,不要忘记 /**表示,任意多层目录 resources: static-locations: [classpath:/templates] # 写一个的话,会将以前的全部覆盖掉。
重启项目,发现
s1.html 无法访问到
SpringBoot提供的默认目录文件无法访问到
直接 index.html 无法访问到
最前面添加 yjl 之后,才可以访问到。
static-locations 可以配置多个
# 引入 数据库的相关配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true username: root password: abc123 type: com.alibaba.druid.pool.DruidDataSource #配置资源信息 mvc: static-path-pattern: /** # 原先的会清除掉 resources: static-locations: [classpath:/templates,classpath:/s1,classpath:/s2,classpath:/public,classpath:/static] # 配置多个
这个时候, s1目录,s2目录,templates目录, public目录,static目录均可以进行访问。
但是 resources目录并没有配置,所以无法访问到(不知道为什么,d.html竟然可以访问到)。
b可以访问到:
c无法访问到:
通常采用第一种方式进行配置资源信息。
本章节的代码放置在 github 上:
https://github.com/yuejianli/springboot/tree/develop/Static
谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!