百里香获得前三个对象

时间:2018-11-28 18:45:44

标签: java spring spring-mvc spring-boot thymeleaf

能帮我吗? 我想显示产品中的前3个对象,但不是必须的。 我尝试使用百里香叶序列,但它不起作用。也许有人可以提示我该怎么做。

HTML:

  beforeEach(() => {
    return Promise.all([
        mongo.db('mydb').collection('myCollection1').deleteMany({}),
        mongo.db('mydb').collection('myCollection2').deleteMany({})
      ]) // close the promise.all here
      .then(() => collections.engines().insertOne(exampleObject)) // close `then` here
      .then((value) => {
        foo = value.ops[0];
        return Promise.resolve();
      });
  });

控制器:

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

如果有人可以提示我这个问题,那就太好了。

谢谢。

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:0)

这是使用#{numbers}上下文对象进行操作的方式。

<th:block th:each="i: ${#numbers.sequence(0, 2)}" th:with="product=${products[i]}">
    <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>
相关问题