SpringBoot --thymeleaf引入css无效问题

首先导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

再更改下thymeleaf的版本

<properties>
    <java.version>1.8</java.version>
    <thymeleaf-spring5.version>3.0.9.RELEASE</thymeleaf-spring5.version>
    <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>

声明一个配置类

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
                 //registry.addResourceHandler("/static/").addResourceLocations("classpath:/static/");
                 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
             }
}

注意这里不能加@EnableWebMvc注解

再在yaml文件里配置一下缓存


spring:
  thymeleaf:
    #缓冲的配置
    cache: false
    check-template: true
    check-template-location: true
    #开启MVC thymeleaf 视图解析
    enabled: true
    encoding: utf-8
    mode: HTML5
    prefix: classpath:/templates/
    suffix: .html

切记切记
以css文件为例是以,文件命名不能有多个.如下图是我之前的文件目录
在这里插入图片描述
把.default删掉再运行即可成功加载样式!

最终包目录如下:

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值