Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用Thymeleaf将th:每个元素与模板元素组合_Spring_Templates_Jakarta Ee_Thymeleaf - Fatal编程技术网

Spring 使用Thymeleaf将th:每个元素与模板元素组合

Spring 使用Thymeleaf将th:每个元素与模板元素组合,spring,templates,jakarta-ee,thymeleaf,Spring,Templates,Jakarta Ee,Thymeleaf,我有一个“产品”列表,我想使用html模板将其显示为行表列表 html模板如下所示: <tr th:fragment="productTemplate"> <td th:text="${productName}">product name</td> <td th:text="${productprice}>product price</td> </tr> 产品名称 th:包括是你正在寻找的。下面的代码适用

我有一个“产品”列表,我想使用html模板将其显示为行表列表

html模板如下所示:

<tr th:fragment="productTemplate">
    <td th:text="${productName}">product name</td>
    <td th:text="${productprice}>product price</td>
</tr>

产品名称

th:包括是你正在寻找的。下面的代码适用于我。我更喜欢把多个片段放在一个文件中,所以我把它包括在这里

<table>
    <tr th:each="product : ${products}" th:include="/fragments/productFragment :: productRow" />
</table>
...

/fragments/productFragment.html
...
<tr th:fragment="productRow">
    <td th:text="${product.productName}">product name</td>
    <td th:text="${product.productPrice}">product price</td>
</tr>
...

...
/片段/productFragment.html
...
产品名称
产品价格
...
我明白了:

<table>
  <tr th:each="product : ${products}" th:include="product :: productTemplate" 
      th:with="productName=${product.name}, productPrice=${product.price}" 
      th:remove="tag" />
</table>

在这里,我们可以将模板类保留在tr元素上(这正是我想要的)


产品名称
产品价格
结果如下:

<table>
  <tr class="my-class">
    <td>Lettuce</td>
    <td>12.0</td>
  </tr>
  <tr class="my-class">
    <td>Apricot</td>
    <td>8.0</td>
  </tr>
</table>

生菜
12
杏
8
感谢来自

<tbody th:fragment="productTemplate">
  <tr class="my-class">
    <td th:text="${productName}">product name</td>
    <td th:text="${productPrice}">product price</td>
  </tr>
</tbody>
<table>
  <tr class="my-class">
    <td>Lettuce</td>
    <td>12.0</td>
  </tr>
  <tr class="my-class">
    <td>Apricot</td>
    <td>8.0</td>
  </tr>
</table>