【问题标题】:How to add a break or new line in Thymeleaf?如何在 Thymeleaf 中添加换行符或换行符?
【发布时间】:2020-04-19 21:59:39
【问题描述】:

这是我使用 Thymeleaf 模板的代码

<form>
     <label>
          <input type="checkbox" th:each="activeMeal : ${activeMeals}" th:text="${activeMeal.itemName}">
     </label>
</form>

这是正在生成的:

我希望每个复选框在单独的行中。在普通的纯 HTML 中,我会使用 &lt;br/&gt; 标签来创建中断。但它似乎在 Thymeleaf 标签内不起作用。

【问题讨论】:

  • 不要这样做,但是...可以使用 th:utext 来避免在字符串中转义 HTML。见this question。我说不要这样做,因为有风险。请参阅答案的 cmets 之一中的注释:“...使用 utext 会使您容易受到跨站点脚本攻击...”。我想出于这个原因,最好永远使用utext 方法 - 即使您认为您的特定用例可能是安全的。

标签: java html spring spring-boot thymeleaf


【解决方案1】:
<th:block th:each="activeMeal : ${activeMeals}">
    <input type="checkbox" name="selectedActive" id="selectedActive" th:value="${activeMeal.itemName}"/>
    <label th:text="${activeMeal.itemName}"></label>
    <br/>
</th:block>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    相关资源
    最近更新 更多