【问题标题】:How to load corectly image in thymeleaf (spring boot project)?如何在百里香(春季启动项目)中正确加载图像?
【发布时间】:2020-01-25 22:56:02
【问题描述】:

我想在我的 Spring Boot 应用程序发生错误后显示图像。我阅读了文档并尝试了多种方法,但仍然无法正常工作。

我阅读了文档并尝试了多种方法,但仍然无法正常工作。 我试试这个:

<img th:src="@{/images/error_image.jpg}"/>

还有这个:

<img src="../static/images/error_image.jpg" th:width="1000" th:src="@{/images/error_image.jpg}"/>

这是我的项目结构:

project structure

使用 ctrl 将鼠标悬停在照片上会将我引导至照片,因此路径很好。

link

但我总是看到这个:

error

【问题讨论】:

标签: image spring-boot thymeleaf src


【解决方案1】:

[编辑]

问题在于下面的控制器方法,您应该在下面提供映射。

@GetMapping   // add mapping here
public String showSignUpForm()
{
    return "register";
}

确保静态资源未配置到其他路径 static/ 最后。 如果您在 application.properties 中有以下配置,请将其删除。

spring.resources.static-locations=

确保您在application.properties 中有以下配置,spring boot 的自动配置会将其设置为resource/static/

spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

这不是问题,但仍将/images/ 更改为images/

<img src="../static/images/error_image.jpg" th:width="1000" th:src="@{images/error_image.jpg}"/>

【讨论】:

  • 感谢您的回答。我尝试了您所说的所有内容,但仍然无法正常工作,但是当我输入页面源并按下图像时,它会将我重定向到我的注册页面。也许这将有助于分析错误。 [注册] (i.imgur.com/u8MZS0T.jpg)
  • @PawelPamula 如果可能,请将代码推送到 GitHub 并分享链接。
  • 在 index.html 中我想加载图片 [github] (github.com/Volumen/training-project-pp)
  • @PawełPamuła 我已经编辑了答案,问题在于您的请求映射方法之一。
  • 谢谢,现在我知道为什么会这样了:)
猜你喜欢
  • 2019-01-05
  • 2018-11-27
  • 2018-05-19
  • 2015-03-12
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
相关资源
最近更新 更多