Thymeleaf if + 每个订单

dtr*_*unk 2 java spring thymeleaf

我正在做以下事情:

<p th:if="${foo != null}" th:each="row : ${foo.values().iterator().next().rowKeySet()}">
Run Code Online (Sandbox Code Playgroud)

foo是 的一个实例java.util.Map

Thymeleaf throws an `TemplateProcessingException: 
Exception evaluating SpringEL expression: "foo.values().iterator().next().value.rowKeySet()"` with root cause `SpelEvaluationException: EL1011E:(pos 22): 
Method call: Attempted to call method values() on null context object`.
Run Code Online (Sandbox Code Playgroud)

为什么 Thymeleafth:each在结果为th:ifis时进行处理false以及如何解决?

这是我目前肮脏的解决方法:

<p th:if="${foo != null}" th:each="row : ${foo != null ? foo.values().iterator().next().rowKeySet() : null}">
Run Code Online (Sandbox Code Playgroud)

Lea*_*edo 5

Thymeleafth:each之前处理th:if因为定义了属性优先级,它建立了评估标签的顺序,这在这里解释。

作为一项工作,您可以包装th:each表达式,例如:

<div th:if="${foo != null}">
    <p th:each="row : ${foo.values().iterator().next().rowKeySet()}">
    ...
Run Code Online (Sandbox Code Playgroud)

我不知道您工作的上下文,但作为参考,您可以使用 Thymeleaf ( docs )轻松迭代地图。