首页 > 解决方案 > 属性 th:each 在这里不允许(Thymeleaf 模板中的错误)

问题描述

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

运行下面的 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

标签: springspring-bootthymeleaf

解决方案


您使用与列表变量 ( 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>

推荐阅读