spring - 属性 th :each is not allowed here(Error in Thymeleaf template)

标签 spring spring-boot 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

最佳答案

您正在使用与列表变量相同的变量名进行迭代 (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>

关于spring - 属性 th :each is not allowed here(Error in Thymeleaf template),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51352086/

相关文章:

spring - session /实体管理器已关闭

java - Spring 启动 org.springframework.beans.factory.BeanCreationException : Could not autowire field:

spring - 如何将图像添加到 thymeleaf

java - 如何在不更改 URL 的情况下从 Controller 调用方法到 thymeleaf

java - Spring ajax 400(错误请求)

java - 无法使用 Spring Boot 和 JPA 在 MySQL 8 DB 中插入数据

java - 我如何让我的 Spring-JUnit 测试认为它在 GenericApplicationContext 中运行?

java - 使用 MockBean 清理 Spring Context 缓存失败 loadLibrary

java - 如何为非嵌入式服务器定义 spring-boot 应用程序属性?

java - 如何检查 Thymeleaf 片段是否已定义