代码之家  ›  专栏  ›  技术社区  ›  dtrunk

Thymeiaf如果+每个订单

  •  3
  • dtrunk  · 技术社区  · 11 年前

    我正在执行以下操作:

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

    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`.
    

    为什么Thymelaf流程 th:each th:if false 如何解决?

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

    <p th:if="${foo != null}" th:each="row : ${foo != null ? foo.values().iterator().next().rowKeySet() : null}">
    
    1 回复  |  直到 11 年前
        1
  •  5
  •   Leandro Carracedo    11 年前

    Thymelaf过程 th:each 之前 th:if 因为定义了属性优先级,它确定了评估标记的顺序 here .

    作为一项工作,您可以将 th:每个 表达式,例如:

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

    我不知道您正在工作的上下文,但作为参考,您可以使用Thymelaf轻松地遍历地图( docs ).