使用Thymeleaf创建自定义的mailto链接

8

我正在使用Spring和Thymeleaf开发项目。

我有一份用户列表。我想创建一个使用mailto:的联系链接。

<a href="mailto:name@email.com">

我是这样展示用户列表的:

<tr th:each="users : ${users}">
    <td th:text="${users.id}"></td>
    <td th:text="${users.firstname}"></td>
    <td th:text="${users.lastname}"></td>
    <td th:text="${users.email}"></td>
</tr>

我希望实现的目标是让mailto适用于每一个新教授和他们的电子邮件,而不需要我手动编写。我在考虑是否可能编写类似以下这样的内容:

<td><a th:href = "mailto:${users.email}">
2个回答

26

请尝试以下代码:

<td><a th:href="'mailto:' + ${users.email}">Send email </a></td>

就这样了!谢谢 :) - user8421021
请看我的答案,了解如何使用链接语法(框架执行适当的转义)安全地完成此操作。 - Marcin Wisnicki

3

您可以使用链接语法更安全地执行此操作,并使用额外的参数:

<td><a th:href="@{mailto:{to}(to=${users.email})}">Send email </a></td>

这样您也可以传递CC,主题和正文:

<td><a th:href="@{mailto:{to}(to=${users.email},subject=${subj},body=${body})}">Send email </a></td>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接