You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Thymeleaf静态目录图片无法显示问题求助

解决Spring Boot + Thymeleaf图片404无法显示问题

修正路径写法

把图片路径改为带根路径的写法,避免相对路径拼接错误:

<img th:src="@{/images/pipe.png}" width="20px" height="20px" alt="Logo">

注:@{images/pipe.png}是相对路径,会根据当前页面URL拼接(比如当前页面是http://localhost:8080/image,会生成http://localhost:8080/image/images/pipe.png),而@{/images/pipe.png}是绝对根路径,会直接映射到resources/static/images/pipe.png。

检查静态资源配置

确认Spring Boot默认静态资源规则未被覆盖:

  • 查看application.properties/application.yml,如果存在spring.web.resources.static-locations配置,确保包含classpath:/static/,比如:
    spring.web.resources.static-locations=classpath:/static/,classpath:/public/
    

验证资源打包情况

  • 使用Maven执行mvn clean package重新构建项目,解压生成的jar包,检查BOOT-INF/classes/static/images/下是否存在pipe.png;
  • IDE开发时,确认resources目录被标记为资源根目录:IntelliJ IDEA右键resources -> Mark Directory as -> Resources Root;Eclipse中设置resources为源文件夹。

排查控制器拦截问题

如果自定义了WebMvcConfigurer,确保静态资源未被错误拦截:

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

同时检查自定义拦截器,确认未拦截/images/**这类静态资源请求。

清理浏览器缓存

按Ctrl+Shift+R强制刷新页面,或使用隐私模式访问,避免浏览器缓存旧的404响应。

内容的提问来源于stack exchange,提问作者Colony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 06:30:14