Thymeleaf、SpringBoot2.x过后static下的静态资源无法访问的终极解决方案

网上试了很多办法,更改配置文件加拦截器什么的都试了,最后总结一下是因为SpringBoot版本的问题,但是使用Thymeleaf的模板语法就能找到文件。
解决方案一
代码全部使用Thymeleaf的模板语法,比如th:src="@{'/css/my.css'}"
解决方案二
加一个路径配置文件

package com.hrh.blog.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置静态资源映射
 *
 * @author Heerh
 * @version 1.0
 * @date 2020/6/21 12:48
 **/
@Component
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 添加静态资源文件,外部可以直接访问地址
     *
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

前段路径格式:

<img src="../static/images/heardImg.jpg" alt="" class="ui avatar image" />

我的项目路径(供参考)
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值