最近用springboot+thymeleaf的时候又发现在运行的时候,静态资源无法展示,网上搜了好多方法:
在application.properties文件中添加 spring.mvc.static-path-pattern=/static/**
spring.mvc.static-path-pattern=/static/**
添加以下类:
1 import org.springframework.context.annotation.Configuration; 2 import org.springframework.util.ResourceUtils; 3 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 4 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 5 6 @Configuration 7 public class WebConfig extends WebMvcConfigurerAdapter { 8 public void addResourcesHandlers (ResourceHandlerRegistry registry){ 9 registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/"); 10 registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/"); 11 super.addResourceHandlers(registry); 12 } 13 }
除了以上方法我还发现,更改文件名使其尽量短,有时也可以解决该问题。
springboot+thymeleaf不能读取静态资源的问题
原文:https://www.cnblogs.com/hong-yf/p/14654757.html