首页 > 解决方案 > 我如何计算表行数并在 Spring Boot 中显示输入表单?

问题描述

我正在使用带有百里香的弹簧靴。我是弹簧靴的新手。我有一个名为 Menu 的表,其中包含 column(id, menuname, url) 我已经为此表完成了 CRUD 操作。现在我想计算行数并以输入形式显示。例如,如果我有 5 行,我想让我的下拉框使用选项 1,最多行数为 1、2、3、4、5,如果我的行是 7,那么百里香下拉列表将为 1, 2,3,4,5,6,7

在我的菜单服务课上,我试试这个

@Transactional(readOnly = true)  
public long getCountOfEntities() {
    long count = menuRepository.count();
    return count;
}

我只是卡在这里...请帮助我。

标签: htmlspringspring-bootthymeleaf

解决方案


您需要在 HTML 中为您的选择生成选项。
尝试使用 th:each 和 #numbers.sequence(from, to)

<select>
    <option th:each="i : ${#numbers.sequence(1, count)}" th:value="${i}" th:text="${i}">
    </option>
</select>

推荐阅读