我正在设置一个 API 来发送电子邮件。我正在使用 Thymeleaf 的模板引擎来格式化我的电子邮件正文。
我无法将值传递给 HTML 文件中的变量。
我试过使用 setVariable
函数。
Context context = new Context();
//context.setVariable("variable in html", "value to it");// set like this
context.setVariable("name", "ajay");
context.setVariable("date", "23rdfeb");
context.setVariable("rupee", 399);
String sendDay1Email = templateEngine.process("lifecyclemailer/sample", context);
oMessage.get().setText(sendDay1Email, true);
电子邮件模板:
Dear `${name}`, your pack will expire on `${date}` of `${rupee}`.
这是我应该收到的电子邮件。
电子邮件模板:
Dear Ajay, your pack will expire on 23rd Feb of 2019.
这是我期待的电子邮件。
最佳答案
通常,Thymeleaf 表达式仅在 HTML 标记属性(如 <span th:text="${name}" />
)或内联表达式(如 [[${name}]]
)中处理。您的模板可能需要如下所示:
电子邮件模板:
Dear [[${name}]], your pack will be expired on [[${date}]] of [[${rupee}]].
(这受您配置模板引擎的方式影响,但我认为内联表达式适用于所有模式。)
关于java - 如何在 Spring Boot 中使用 Thymeleaf 将值传递给 HTML 模板的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57820442/