spring-mvc - spring boot 不会在页面上显示对象列表
问题描述
我有一个使用 SpringBoot 构建的简单 Web 应用程序,但在页面上显示对象列表时遇到问题:
这是我的模型:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class Slot {
private Long id;
private DateTime startTime;
private DateTime finishTime;
private String title;
private String description;
}
控制器:
@RestController
public class AdminController {
@GetMapping("/admin/slots")
public ModelAndView getSlots() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("admin/slots");
List<Slot> slots = .. get slots from other service ..
modelAndView.addObject("slots", slots);
return modelAndView;
}
}
看法:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Slot List</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table class="table" id="slot-table">
<thead>
<tr>
<th>id</th>
<th>start time</th>
<th>finish time</th>
<th>title</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr th:each="slot : ${slots}">
<td th:text="${slot.getId()}"></td>
<td th:text="${slot.getStartTime()}"></td>
<td th:text="${slot.getFinishTime()}"></td>
<td th:text="${slot.getTitle()}"></td>
<td th:text="${slot.getDescription()}"></td>
</tr>
</tbody>
</table>
</body>
</html>
当我在浏览器中打开时,我可以看到表格标题但看不到表格中的数据,我做错了什么吗?
解决方案
要访问视图页面中的字段,您不必使用 getter 和 setter。只需使用点运算符访问,如下所示。
${slot.id}
${slot.startTime}
${slot.finishTime}
${slot.title}
${slot.description}
推荐阅读
- java - 当我在 `layout.xml` 文件中添加工具栏时,Android Studio RecyclerView 不会显示我的数据
- javascript - Axios 获得不同的 X-CSRF 令牌,然后是 Postman
- azure-active-directory - 通过图表添加的用户未收到欢迎电子邮件
- python-3.x - CNN 加载模型不适用于测试数据集
- python - 我收到 PermissionError: [Errno 13] Permission denied: when move a file using shutil.move 有没有办法忽略这个错误?
- android - 使用 android gradle 插件 3.6+ 执行 crashlyticsGenerateSymbols 任务时出错
- python - 我无法使用 Python 从 JSON 响应中正确访问字典列表
- blazor - 动态附加 Blazor 组件
- vue.js - ckeditor4-vue 与 Nuxt.js 如何访问 CKEDITOR
- python - 使用 Python 对 mongodb 进行日期查询返回空