Spring mvc ThymalLeaf |参数化段在一个th中:每个
我试图在th:each中使用参数化段,但遇到以下异常: EL1007E:在null上找不到属性或字段“author” 如果我得到了正确的答案,这意味着我试图访问其变量的对象为空,尽管通过SpringMVC应用程序中的断点和调试,我确信列表中有两个元素Spring mvc ThymalLeaf |参数化段在一个th中:每个,spring-mvc,thymeleaf,Spring Mvc,Thymeleaf,我试图在th:each中使用参数化段,但遇到以下异常: EL1007E:在null上找不到属性或字段“author” 如果我得到了正确的答案,这意味着我试图访问其变量的对象为空,尽管通过SpringMVC应用程序中的断点和调试,我确信列表中有两个元素 这是控制器: @GetMapping("/") public String getHomePage(Model model) { log.info("Recupero la home-page");
这是控制器:
@GetMapping("/")
public String getHomePage(Model model) {
log.info("Recupero la home-page");
model.addAttribute("reviews", mainService.getAllReviews());
return "home";
}
以下是th:each:
<div
th:each="review: ${reviews}"
th:assert="({review} != null)"
th:replace="fragments/utilities :: test(author=${review.author},message=${review.review})"
></div>
以下是片段:
<div th:fragment="test(author, message)">
<p th:text="${message}" class="mt-2 text-dark"></p>
<h6 th:text="${author}"></h6>
</div>
以下是将网页返回给客户端之前运行时模型的屏幕截图:
怎么了?为什么它说查看对象为空?您的主Thymeleaf模板有问题。您需要考虑哪个指示将首先运行
th:replace
,然后是th:each
,并替换整个
元素。工作代码可能类似于
<th:block th:each="review: ${reviews}">
<div th:replace="fragments/utilities :: test(author=${review.author},message=${review.review})"></div>
</th:block>
您的主Thymeleaf模板存在问题。您需要考虑哪个指示将首先运行
th:replace
,然后是th:each
,并替换整个
元素。工作代码可能类似于
<th:block th:each="review: ${reviews}">
<div th:replace="fragments/utilities :: test(author=${review.author},message=${review.review})"></div>
</th:block>