首页 > 解决方案 > 如何在 Thymeleaf 中将 th:text 和静态内容与 html 标签连接起来?

问题描述

<div th:switch="${data.totalPercentage}">
    <td th:case="100" th:text="${data.totalPercentage}"/>
    <td th:case="*" th:text="${data.totalPercentage + '*'}" class="alert alert-warning font-weight-bold"/>
</div>

上面的表达式有效,但我无法将 html 标记与 th:text 连接起来。我想用 fontawesome flag icon 替换 * <i class="fas fa-flag"></i>。有什么建议吗?

标签: spring-bootthymeleafspring-boot-test

解决方案


没有真正的理由在这里连接,您应该考虑 HTML。

<div th:switch="${data.totalPercentage}">
  <td th:case="100" th:text="${data.totalPercentage}"/>
  <td th:case="*" class="alert alert-warning font-weight-bold">
    <span th:text="${data.totalPercentage}" /> <i class="fas fa-flag"></i>
  </td>
</div>

推荐阅读