【问题标题】:Spring + thymeleaf html email templates, how to add html tags in messages?Spring + thymeleaf html邮件模板,如何在邮件中添加html标签?
【发布时间】:2017-05-27 15:15:33
【问题描述】:

我想使用 Thymeleaf 引擎从我的应用程序发送 html 电子邮件。我可以发送 html 电子邮件,当我想绑定到模板时,问题就来了,比方说

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
    <span th:text="${message}"></span>


</body>
</html>

${message} 来自控制器,它是国际化消息,所以我以这种方式得到它

String message = messageSource.getMessage("email_message", null, locale);

email_message 在我的 messages_xx.properties 文件中,具体取决于 locale message可以加html标签吗?

如果我写类似

Hello <b>User</b><br/>Welcome!

在电子邮件中我从字面上收到了这条消息,因为标签没有被解析

【问题讨论】:

    标签: html spring email thymeleaf


    【解决方案1】:

    试试Unescaped Text:

    <span th:utext="${message}"></span>
    

    注意text->utext 改变了。

    它不会转义html特殊字符(如&amp;lt;&gt;&amp;),也就是说,它不会将它们转为&amp;lt;,'>','&',所以它输出逐字逐句。

    请注意,如果您插入的文本不是来自绝对可信的来源,这样的配置会给 XSS 攻击留下漏洞。当然,如果您只想从您自己编写的属性文件中插入一些文本,那也可以。

    【讨论】:

    • 谢谢,我在 Spring 中寻找一些东西,没有直接想到 Thymeleaf!
    猜你喜欢
    • 2020-11-21
    • 2015-05-29
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多