spring - 属性 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
解决方案
您使用与列表变量 ( 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>
推荐阅读
- python - 如何查看用户是否处于多对多关系中?
- django - 使用 json.dumps 和普通 python 字典发送数据有什么区别?
- spring - 验证字段何时相互依赖
- java - 从 .xlsx 读取特定工作表并写入另一个 .xlsx
- javascript - 通过 websocket 发送 blob 导致页面重新加载
- javascript - 添加图像以响应动态链接
- r - 使用 ggplot 的时间序列数据:如何为每个时间点使用不同的颜色并与属于每个主题的线条数据连接?
- mysql - 尝试上传图像时 Multer 出错
- java - Android在某些部分显示文本语言错误
- python - 图像下载但字节大小为 0 同时响应是 respons.ok 和 python 中的 200 代码?