胸腺-参数化片段-如何将数据从模板发送到布局,然后再发送到片段



我想问一些关于Thymelaf中参数化片段的问题。我有homepage.html,它是thymelaf模板,我通过Controller(Java,Spring(将模型发送到它。该模型具有这样的值:;所有评论:listOfComments"。listOfComments的类型为List,在.中包含Comment对象

我想用这个";AllComments"片段中的变量";评论";。(打印表格(。如何将这个变量从homepage.html通过layout.html发送到fragments.html中的fragment?谢谢你的回答。

<!-- 
folder : ..../templates 
homepage.html
to this template i am sending Model model with: "AllComments: listOfComments".
listOfComments is varirable of type List<Comments> with Comment objects in.
-->
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout/layoutPage.html}"
lang="sk">
<head>
<title>My Page</title>
</head>
<body>
<main layout:fragment="content">
</main>
</body>
</html>
<!-- 
folder: ..../layout
layoutPage.html  -->
<!DOCTYPE html>
<html lang="sk"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>

<main layout:fragment="content"></main>

<div th:replace="fragments/fragments :: comments" />

<footer th:replace="fragments/fragments :: footer" />
</body>
</html>
<!-- 
folder: ..../fragments
fragments.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="sk">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>



<!--fragment footer-->
<footer th:fragment="footer">
<hr>
<p>Footer: xxx</p>
<hr>
</footer>
<!--fragment comment-->
<div th:fragment="comments(AllComments)">
<table class="comments">
<thead>
<tr>
<th> Username </th>
<th> Comment </th>
<th> Commented On</th>
</tr>
</thead>
<tbody>
<tr th:if="${AllComments.empty}">
<td colspan="2"> No Comments Available </td>
</tr>
<tr th:each="item : ${AllComments}">
<td><span th:text="${item.username}"> </span></td>
<td><span th:text="${item.comment}">  </span></td>
<td><span th:text="${item.commentedOn}"> </span></td>
</tr>
</tbody>
</table>
</div>

</body>
</html>

只需将变量传递到使用片段的位置:

<div th:replace="fragments/fragments :: comments(${AllComments})" />

相关内容

最新更新