springboot+thymeleaf不能读取静态资源的问题

最近用springboot+thymeleaf的时候又发现在运行的时候,静态资源无法展示,网上搜了好多方法:

方法一:

在application.properties文件中添加 spring.mvc.static-path-pattern=/static/**

spring.mvc.static-path-pattern=/static/**

方法二:

添加以下类:

 1 import org.springframework.stereotype.Component;
 2 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 3 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 4 
 5 
 6 
 7 @Component
 8 public class WebConfig implements WebMvcConfigurer {
 9 /*
10      * 添加静态资源文件,外部可以直接访问地址
11      *
12      * @param registry
13      */
14 
15     @Override
16     public void addResourceHandlers(ResourceHandlerRegistry registry) {
17         registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
18     }
19 }

除了以上方法我还发现:

1、更改文件名使其尽量短;

2、不要在idea工具里面直接粘贴图片,直接拷贝到磁盘里的文件夹下

有时也可以解决该问题。

原文地址:https://www.cnblogs.com/hong-yf/p/14654757.html