记录个问题,先说背景:springboot项目中使用了thymeleaf模板文件,在一个模板文件中需要访问一个静态图片,结果图片死活访问不通。
模板文件中图片引入代码如下:
<!-- 前面代码 -->
<div id="logo" class="col-xs-2 text-left">
<img th:src = "@{/static/images/logo-std-baidu.png}" />
</div>
<!-- 后边的代码 -->本地静态图片文件的路径如下:

解决过程:1. 一开始尝试在配置文件中去配置如下,结果还是访问不到图片。
spring.mvc.static-path-pattern: /static/**
spring.resources.static-locations: classpath:/static/2.翻阅thymeleaf官方文档,手动写了如下的配置类,能够访问到图片。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
@Configuration
public class ResourceConfig implements WebMvcConfigurer {
//
//
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ResourceHandlerRegistry()).excludePathPatterns("/static/**");
}
//
@Override
//需要告知系统,这是要被当成静态文件的!
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 设置文件上传的文件不拦截
// registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ TaleUtils.getUplodFilePath()+"upload/");
//第一个方法设置访问路径前缀,第二个方法设置资源路径
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
@Autowired
private ApplicationContext applicationContext;
@Bean
public SpringResourceTemplateResolver templateResolver(){
// SpringResourceTemplateResolver automatically integrates with Spring's own
// resource resolution infrastructure, which is highly recommended.
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
// HTML is the default value, added here for the sake of clarity.
templateResolver.setTemplateMode(TemplateMode.HTML);
// Template cache is true by default. Set to false if you want
// templates to be automatically updated when modified.
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine(){
// SpringTemplateEngine automatically applies SpringStandardDialect and
// enables Spring's own MessageSource message resolution mechanisms.
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
// Enabling the SpringEL compiler with Spring 4.2.4 or newer can
// speed up execution in most scenarios, but might be incompatible
// with specific cases when expressions in one template are reused
// across different data types, so this flag is "false" by default
// for safer backwards compatibility.
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
}反思:为什么方法 1 行不通:
翻阅项目,发现项目中存在一个别人写的类WebXML,该类中有addResourceHandlers方法,代码如下

于是将上面代码中的addResourceHandlers重新改为如下,图片访问可以成功。
@Override
//需要告知系统,这是要被当成静态文件的!
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//第一个方法设置访问路径前缀,第二个方法设置资源路径
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}再探讨一下,如下配置到底是什么意思:
spring.mvc.static-path-pattern代表的含义是我们应该以什么样的路径来访问静态资源,换句话说,只有静态资源满足什么样的匹配条件,Spring Boot才会处理静态资源请求,以官方配置为例:
# 这表示只有静态资源的访问路径为/resources/**时,才会处理请求
spring.mvc.static-path-pattern=/resources/**,假定采用默认的配置端口,那么只有请求地址类似于“http://localhost:8080/resources/jquery.js”时,Spring Boot才会处理此请求,处理方式是将根据模式匹配后的文件名查找本地文件,那么应该在什么地方查找本地文件呢?这就是“spring.resources.static-locations”的作用了。
spring.resources.static-locations”用于告诉Spring Boot应该在何处查找静态资源文件,这是一个列表性的配置,查找文件时会依赖于配置的先后顺序依次进行,默认的官方配置如下:
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources继续以上面的请求地址为例,“http://localhost:8080/resources/jquery.js”就会在上述的四个路径中依次查找是否存在“jquery.js”文件,如果找到了,则返回此文件,否则返回404错误。
结论:
“spring.mvc.static-path-pattern”用于阐述HTTP请求地址,而“spring.resources.static-locations”则用于描述静态资源的存放位置。
---------------------
有关spring.mvc.static-path-pattern和spring.resources.static-locations的配置参照了如下文章,感谢他的文章对我理解其中知识所起的帮助。
作者:蚁方阵
原文:https://blog.csdn.net/yiifaa/article/details/78299052