You need to enable JavaScript to run this app. Spring Boot应用使用Thymeleaf模板时网页图片无法显示该如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用使用Thymeleaf模板时网页图片无法显示该如何解决

你的图片加载失败是因为Thymeleaf模板中静态资源路径写法不符合规范导致的。

你需要把原生的src属性替换为Thymeleaf提供的th:src属性,同时使用@{/}前缀的URL表达式来引用静态资源,这种写法会自动适配项目的上下文路径,不会出现相对路径匹配错误的问题。

正确代码示例:

<span class="Google">
    <a href="">
        <img th:src="@{/img/google.png}" alt="Google">
    </a>
</span>

<span class="kakao">
    <a href="">
        <img th:src="@{/img/kakao.png}" alt="Kakao">
    </a>
</span>

额外注意事项:

  • 确认img文件夹存放在项目的静态资源根目录下,SpringBoot项目默认静态资源根目录为src/main/resources/static,即图片的实际存放路径应为src/main/resources/static/img/google.png
  • 必须启动服务后通过服务端口访问页面,直接打开本地HTML文件的情况下Thymeleaf的属性不会被解析,图片依旧无法加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 16:33:00