Thymeleaf参数化片段:如何将数据从模板经布局传递至目标片段
AllComments to Thymeleaf Parametrized Fragment Let's break down how to get your AllComments list from the homepage's Model into the comments fragment through your layout template. The core issue right now is that your parameterized comments fragment expects an AllComments argument, but you're not passing it when you call the fragment in your layout.
Step 1: Pass the AllComments Variable to the Fragment in Layout
Your layout template (layoutPage.html) is where you include the comments fragment. Since the AllComments variable is already available in the global Thymeleaf context (from your controller's Model), you just need to explicitly pass it to the fragment when you call it.
Update the fragment call in layoutPage.html from:
<div th:replace="fragments/fragments :: comments" />
To:
<div th:replace="fragments/fragments :: comments(AllComments=${AllComments})" />
This tells Thymeleaf to take the AllComments variable from the context and pass it into the fragment's AllComments parameter.
Step 2: Fix Minor Layout Issue in the Fragment
I noticed a small bug in your fragment's empty state row: you set colspan="2" but your table has 3 columns. This will cause layout issues when there are no comments. Update that line in fragments.html, and use Thymeleaf's built-in utility for more reliable null/empty checks:
From:
<tr th:if="${AllComments.empty}"> <td colspan="2"> No Comments Available </td> </tr>
To:
<tr th:if="${#lists.isEmpty(AllComments)}"> <td colspan="3"> No Comments Available </td> </tr>
The #lists utility handles null values gracefully, making your template more robust.
Full Modified Code Snippets
Updated 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> <!-- Explicitly pass AllComments to the comments fragment --> <div th:replace="fragments/fragments :: comments(AllComments=${AllComments})" /> <footer th:replace="fragments/fragments :: footer" /> </body> </html>
Updated fragments.html (comments section)
<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="${#lists.isEmpty(AllComments)}"> <td colspan="3"> 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>
How It Works
Thymeleaf shares the same context across all templates, layouts, and fragments. When your controller adds AllComments to the Model, it's accessible everywhere in your template hierarchy. By explicitly passing it to the parameterized fragment, you ensure the fragment has access to the data it needs to render the table correctly.
内容的提问来源于stack exchange,提问作者LuckyLuke

