社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
productList : 后台model 对象中存储的集合对象
product : 集合中单个对象的引用
product.description :单个对象的单个属性
循环其实就是 java 中foreach 差不多的写法
// 跟java 中的这种循环是一致的
for (Object o : Lists.newArrayList()) {
}
在thymeleaf 中的写法示例
<tr th:each="product : ${productList}">
<td th:text="${product.description}">Red chair</td>
<td th:text="${'¥' + #numbers.formatDecimal(product.price, 1, 2)}">¥350</td>
<td th:text="${#dates.format(product.availableFrom, 'dd-MM-yyyy')}">2018-02-20</td>
</tr>
thymeleaf 会在创建循环时,给你创建一个 引用名称 + Stat 的变量,用来获取相关的属性
下面是示例代码
<tr th:each="product : ${productList}">
<td th:text="${productStat.count}">1</td>
<td th:text="${product.description}">Red chair</td>
<td th:text="${'¥' + #numbers.formatDecimal(product.price, 1, 2)}">¥350</td>
<td th:text="${#dates.format(product.availableFrom, 'dd-MM-yyyy')}">2018-02-20</td>
</tr>
Stat 这个类型会提供给你如下几个属性,你可以根据需要进行操作
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!