Thymeleaf:<div> 中的循环迭代标记并创建行. [英] Thymeleaf: Loop iteration in <div> tag and create rows.

查看:41
本文介绍了Thymeleaf:<div> 中的循环迭代标记并创建行.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的 Spring MVC 控制器代码

ProductCategory category = new ProductCategory();category.setId(BigInteger.valueOf(558711));category.setTitle("类别 1");category.setStatus(FinEnum.STATUS.IN_ACTIVE.getStatus());ProductCategory category2 = new ProductCategory();category.setId(BigInteger.valueOf(558722));category.setTitle("类别 2");category.setStatus(FinEnum.STATUS.ACTIVE.getStatus());列表<产品类别>category = new ArrayList();类别.添加(类别);类别.添加(类别2);model.addAttribute("类别", 类别);返回管理员/产品/查看类别";

我的百里香代码:

<div class="column2 quota-date" style="width: 15%;"><span th:text="${category.id}">虚拟数据</span></div><div class="column2 tax-date" style="width: 15%;"><span th:text="${category.title}">虚拟数据</span></div><div class="column2 quota-date" style="width: 13%;"><span th:text="${category.status}">虚拟数据</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;">查看子类别</a><a class="icon2" href="javascript:void(0)"><p>编辑</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>

<a class="icon14" href="javascript:void(0)" style="width: 60px;"><p>删除</p></a>

我的输出是:

解决方案

问题与 Thymeleaf 无关,只是一个简单的错字.行后:

ProductCategory category2 = new ProductCategory();

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

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

在本地对此进行了测试,并在修复后看到了行"数据.

I am new in thymeleaf, and trying to iterate values using thymeleaf th:each attribute, but get wrong output. I am using <div> instead of table, when thymeleaf render the page, all objects values override the first row values and rest of the rows are empty show. Following is my code:

My Spring MVC controller code

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";

My thymeleaf code:

<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>

My Output is:

解决方案

The problem has nothing to do with Thymeleaf, it's just a simple typo. After the line:

ProductCategory category2 = new ProductCategory();

you are still modifying (overwriting) the category object instead of category2. Therefore, the properties of category2 never got set. Corrected code should be:

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

Tested this locally and saw we "rows" of data after the fix.

这篇关于Thymeleaf:&lt;div&gt; 中的循环迭代标记并创建行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆