首页 > 解决方案 > 位于服务器中的 Thymeleaf 图像绑定

问题描述

我需要使用 thymeleaf 读取位于我的 html 中服务器位置的图像,以便在邮件中发送图像,而不是作为附件,而是作为内容。我直接在我的html中使用了这样的东西,

<img alt="imagelogo" th:src = "${imageSrc}/logo.png" width="210" style="display: block;" />

我的java代码如下,

String imgsrc = ""; // Some Path that has been set from my server property file.
final Context ctx = new Context(locale);
ctx.setVariable("imageSrc", imgsrc);

但我遇到了以下异常,

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "${imageSrc}/logo.png"

标签: javaemailthymeleaf

解决方案


这只是你表达的问题。这里有两个应该适合你的例子。使用文字替换

th:src="|${imageSrc}/logo.png|"

或使用标准 url 语法

th:src="@{{source}/logo.png(source=${imageSrc})}"

推荐阅读