Java 如何在springboot中使用Thymeleaf将值传递给HTML模板的变量
我正在设置发送电子邮件的API。我正在使用Thymeleaf的模板引擎来格式化电子邮件的正文 我无法将值传递给HTML文件中的变量 我尝试过使用Java 如何在springboot中使用Thymeleaf将值传递给HTML模板的变量,java,spring-boot,thymeleaf,sendgrid,Java,Spring Boot,Thymeleaf,Sendgrid,我正在设置发送电子邮件的API。我正在使用Thymeleaf的模板引擎来格式化电子邮件的正文 我无法将值传递给HTML文件中的变量 我尝试过使用setVariable函数 Context Context=newcontext(); //setVariable(“html中的变量”,“它的值”);//像这样 setVariable(“name”、“ajay”); setVariable(“日期”、“23rdfeb”); 环境变量(“卢比”,399); 字符串sendDay1Email=templa
setVariable
函数
Context Context=newcontext();
//setVariable(“html中的变量”,“它的值”);//像这样
setVariable(“name”、“ajay”);
setVariable(“日期”、“23rdfeb”);
环境变量(“卢比”,399);
字符串sendDay1Email=templateEngine.process(“lifecyclemailer/sample”,上下文);
omemessage.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.
Dear [[${name}]], your pack will be expired on [[${date}]] of [[${rupee}]].
这是我应该收到的电子邮件
电子邮件模板:
Dear `${name}`, your pack will expire on `${date}` of `${rupee}`.
Dear Ajay, your pack will expire on 23rd Feb of 2019.
Dear [[${name}]], your pack will be expired on [[${date}]] of [[${rupee}]].
这是我所期待的电子邮件。一般来说,Thymeleaf表达式仅在HTML标记属性(如
)或内联表达式(如[[${name}]]]
中处理。您的模板可能需要如下所示:
电子邮件模板:
Dear `${name}`, your pack will expire on `${date}` of `${rupee}`.
Dear Ajay, your pack will expire on 23rd Feb of 2019.
Dear [[${name}]], your pack will be expired on [[${date}]] of [[${rupee}]].
(这受模板引擎配置方式的影响,但我认为内联表达式可以在所有模式下工作。)删除“$name”并替换为“name”已尝试过此方法,但无效。