【问题标题】:Adding button for each elements of list in thymeleaf为百里香中列表的每个元素添加按钮
【发布时间】:2021-04-30 05:43:08
【问题描述】:

我无法将按钮放入基于 Thymeleaf 的列表元素中。我想在列表元素旁边有一个按钮。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout.html}">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
<section layout:fragment="" content="">
    <div class="row">
        <div class="col-2">
            <a th:href="@{/archives}">archiwum zadań</a>
        </div>
        <div class="col-2">
            <a th:href="@{/add}">dodawanie zadania</a>
        </div>
    </div>

    <ul>
        <li th:each="task: ${tasks}"
            th:text="|${task.getId()} ${task.getName()} ${task.getCategory().getDescription()} ${task.isFinished()}|">
            <input type="submit" value="Done">
        </li>
    </ul>
</section>
</body>
</html>

【问题讨论】:

    标签: java html thymeleaf


    【解决方案1】:

    Thymeleaf 将用th:text 表达式生成的任何内容替换任何预先存在的标签内容。

    在您的情况下,预先存在的内容是您的 &lt;input&gt; 元素 - 这就是不显示按钮的原因。

    避免这种情况的一种方法是将您的th:text 放入&lt;li&gt; 内的一个跨度中:

    <ul>
        <li th:each="task: ${tasks}">
            <span th:text="|${task.id} ${task.name} ... |"></span>
            <input type="submit" value="Done">
        </li>
    </ul>
    

    (为简洁起见,我从示例中删除了您的几个字段)。

    请注意,在我的情况下,我已将 ${task.getId} 更改为 ${task.id}。只要您正确命名了 getter(根据 JavaBeans 命名约定),您就可以使用字段名称 - Thymeleaf 会找到要调用的正确 getter。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-12
      • 2019-12-03
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多