我有一个 thymeleaf 模板,它迭代列表(杂务完成对象),并在满足 if 条件时(当杂务完成对象具有特定的 Chore_id 和 person_id 时)打印一封信。这会产生一串字母。如何打印字母的数量而不是重复字母的字符串? thymeleaf 是否能够有一个可以使用的迭代计数器变量?
<tr th:each="chore: ${chores}">
<td>
<span th:each="completion : ${completions}">
<span th:if="${(completion.chore?.chore_id) == chore.chore_id and (completion.person?.person_id) == persons[0].person_id}">T</span>
</span>
</td>
</tr>
这张图片可以阐明意图。
在你的控制器中创建一个数据结构是非常好的,它可以让你轻松输出你需要的内容。
例如,您可以创建一个记录(如果您使用的是较旧的 Java 版本,则创建一个类),其中包含一个用于显示您想要显示的计数的字段,并将此类记录的实例添加到您的模型中:
@GetMapping
public String showChoresOverview(Model model) {
List<ChoresOverviewItemViewModel> items = ... // Get the info from your service and convert those object to ChoresOverviewItemViewModel instances in the controller
mode.addAttribute("items", items);
return "chores/index"; //name of you Thymeleaf template
}
private record ChoresOverviewItemViewModel(String description, String lastCompletionUser, Map<String,Integer> thisMonthUserToCountMapping) {
}