【问题标题】:Attribute th:each is not allowed here(Error in Thymeleaf template)属性 th:each 在这里不允许(Thymeleaf 模板中的错误)
【发布时间】:2018-07-15 20:42:47
【问题描述】:

我创建了将狗信息存储在数据库中的应用程序,同时运行项目时创建的表是创建但狗信息没有更新。

在下面的 html 文件中运行时出错

以下代码无效

<html lang="en">
<head>
    <!-- META SECTION -->
    <title>Dog Rescue</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- END META SECTION -->
    <!--  BEGIN STYLE -->
    <style>
        table, th, td {
            border: 1px solid black;
            padding: 1px;
        }
    </style>
    <!--  END STYLE -->

</head>
<body>
<h2>Current Dogs In Rescue</h2>
<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Rescue Date</th>
        <th>Vaccinated</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="dogs : ${dogs}">
        <td th:text="${dogs.id}">Text ...</td>
        <td th:text="${dogs.name}">Text ...</td>
        <td th:text="${dogs.rescued}">Text ...</td>
        <td th:text="${dogs.vaccinated}">Text...</td>
    </tr>
    </tbody>
</table>
</div>

<h2>Add A Dog</h2>
<form action="#" th:action="@{/}" method="post">
    <label>Name<input type="text" name="name" id="name"></input></label>
    <label>Vaccinated<input type="text" name="vaccinated" id="vaccinated"></input></label>
    <label>Rescued<input type="text" name="rescued" id="rescued"></input></label>
    <input type="submit" value="Submit"></input>
</form>
</body>
</html>

html 文件未获取信息。 请帮助我 整个项目可在 https://github.com/arulsuju/DogRescue.git

【问题讨论】:

  • 在您的 HTML 中添加 w3.org/1999/xhtml" xmlns:th="thymeleaf.org">

标签: spring spring-boot thymeleaf


【解决方案1】:

您使用与列表变量相同的变量名进行迭代 (dogs) , 考虑为迭代变量使用不同的名称,如 (dog),因此代码应为:

<tr th:each="dog : ${dogs}">
    <td th:text="${dog.id}">Text ...</td>
    <td th:text="${dog.name}">Text ...</td>
    <td th:text="${dog.rescued}">Text ...</td>
    <td th:text="${dog.vaccinated}">Text...</td>
</tr>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 2014-05-19
    • 2019-12-04
    • 2021-08-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多