thymeleaf 获取前 3 个对象 [英] thymeleaf get first 3 objects

查看:54
本文介绍了thymeleaf 获取前 3 个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮帮我吗?我想显示我的前 3 个产品对象,我不知道它必须是怎样的.我尝试使用 thymeleaf 序列,但它不起作用.也许有人可以提示我如何做到这一点.

Could you please help me. I want to display my first 3 objects from product, I don't how it must be. I try to use thymeleaf sequence, but it don't work. Maybe somebody can hint me how it could be done.

HTML:

<th:block th:each="product:${products}">

<a  th:class="production_Page" th:href="@{'product/'+${product.id}}"> <p 
th:text="${product.productName}"/></a>

<a th:class="production_Page" 
th:href="@{'productDelete/'+${product.id}}">Delete</a>

<a th:class="production_Page" 
th:href="@{'productEdit/'+${product.id}}">Edit</a>

<img  th:class="productImage" th:src="${product.pathImage}"/>
<br/>
</th:block>

控制器:

@GetMapping("/products")
public String seeAllProductsIntoAList(Model model){
    model.addAttribute("products", productService.findAll());
    model.addAttribute("categories", categoryService.findAll());
    return "/productView/products";
}

如果有人能提示我这个问题就好了.

It would be great if somebody can hint me with this issue.

谢谢.

推荐答案

由于 productsProduct 的列表,您必须遍历该列表.在 thymeleaf 上,您可以使用 th:each 属性进行迭代.因此,对于您的情况,您可以使用以下内容.试试看.

Since products is list of Product, you have to iterate over that list. On thymeleaf you can use th:each attribute to do iteration. So for your case you can use something as below. Give it a try.

<th:each="product,iterStat: ${products}" th:if="${iterStat.index} <3">

我不完全确定,但根据您的问题,您只需要前三个对象.为此,您可以使用 th:each 中定义的状态变量.您可以在此处找到更多详细信息.

I am not entirely sure but based on your question you wanted only first three objects. For this you can use status variable that are defined in th:each. More detail you can find here.

这篇关于thymeleaf 获取前 3 个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆