java - Thymeleaf:在 <div> 标记中循环迭代并创建行。

标签 java spring-mvc thymeleaf

我是 thymeleaf 的新用户,并尝试使用 thymeleaf 迭代值th:each属性,但得到错误的输出。我正在使用<div>当 thymeleaf 渲染页面时,所有对象值都会覆盖第一行值,而其余行则为空显示,而不是表。以下是我的代码:

我的 Spring MVC Controller 代码

ProductCategory category = new ProductCategory();
category.setId(BigInteger.valueOf(558711));
category.setTitle("Category 1");
category.setStatus(FinEnum.STATUS.IN_ACTIVE.getStatus());

ProductCategory category2 = new ProductCategory();
category.setId(BigInteger.valueOf(558722));
category.setTitle("Category 2");
category.setStatus(FinEnum.STATUS.ACTIVE.getStatus());

List<ProductCategory> categories = new ArrayList<ProductCategory>();
categories.add(category);
categories.add(category2);

model.addAttribute("categories", categories);
return "admin/product/view-categories";

我的 thymeleaf 代码:

<div class="row-area" th:each="category: ${categories}">
                    <div class="column2 tariff-date" style="width: 15%;"><span th:text="${category.id}">Dummy Data</span></div>
                    <div class="column2 tariff-date" style="width: 15%;"><span th:text="${category.title}">Dummy Data</span></div>
                    <div class="column2 tariff-date" style="width: 13%;"><span th:text="${category.status}">Dummy Data</span></div>
                    <div class="column5 icons middle-area" style="margin-left: 7px; width: 40%;">
                        <a href="javascript:void(0)" class="diplay-none"></a>
                        <a class="icon7" href="javascript:void(0)" style="width: 140px;">View Sub Category</a>
                        <a class="icon2" href="javascript:void(0)"><p>Edit</p></a>
                        <div th:switch="${category.status}" style="margin-left: 195px;">
                            <a class="icon8" href="javascript:void(0)" th:case="'Inactive'" style="width: 88px;">Deactivate</a>
                            <a class="icon9" href="javascript:void(0)" th:case="'Active'">Active</a>
                        </div>
                        <a class="icon14" href="javascript:void(0)" style="width: 60px;"><p>Delete</p></a>
                    </div>
                </div>

我的输出是:

enter image description here

最佳答案

该问题与 Thymeleaf 无关,这只是一个简单的拼写错误。行后:

ProductCategory category2 = new ProductCategory();

您仍在修改(覆盖)category 对象而不是category2。因此,category2 的属性从未被设置。更正后的代码应该是:

category2.setId(BigInteger.valueOf(558722));
category2.setTitle("Category 2");
category2.setStatus(FinEnum.STATUS.ACTIVE.getStatus());

在本地进行了测试,并在修复后看到了“行”数据。

关于java - Thymeleaf:在 <div> 标记中循环迭代并创建行。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28560999/

相关文章:

java - Bean单例、 session 创建和初始化

java - 表单中的 LocalDate

java - 在 IntelliJ 中运行 JBullet 演示

java - Sonar 规则定义 XML 格式

java - 在查询字符串中发送 XML

java - 如何使用 bootstrap select 在每个 spring mvc3 选择列表中设置所选项目?

java - Spring MVC - 未找到 HTTP 请求的映射

java - Thymeleaf 下拉菜单问题

java - 为什么这个 Spring Boot/Thymeleaf 按钮会隐藏我的数据或根本不起作用?

java - 无法解决 Gradle 依赖项的构建失败异常