【问题标题】:Don't process the each case when if case is true in thymeleaf当百里香叶中的情况为真时,不要处理每种情况
【发布时间】:2020-04-06 14:06:16
【问题描述】:

我正在开发一个 Spring Boot 应用程序。我有以下百里香块:

<Produkt th:if="${visibility}" th:each="user: ${userList.myList}">
    <Name th:text="${user.name}"/>
</Produkt>

我有以下java代码:

@Test
public void myTest() {
    Context context = new Context();
    Map<String, List<Map<String, String>>> userValuesList = new HashMap<>();
    boolean visibility = getVisibility();
    if (visibility) {
        List<Map<String, String>> vv = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("name", "filip");
        vv.add(map);
        Map<String, String> map2 = new HashMap<>();
        map2.put("name", "test");
        vv.add(map2);
        userValuesList.put("myList", vv);
    }
    context.setVariable("visibility", visibility);
    context.setVariable("userList", userValuesList);
    springTemplateEngine.process("test.xml", context);
}

由于我仅在可见性为真时才添加此 myList,因此出现以下错误:

评估 SpringEL 表达式的异常:“userList.myList”

我的问题是只有在可见性为真的情况下,这个 userList.myList 是否可以由 springTemplateEngine 处理?

我想要达到的最终结果是:

<Produkt>
    <Name>filip</Name>
</Produkt>

<Produkt>
    <Name>test</Name>
</Produkt>

如果可见性为假,则整个 Produkt 标记都将消失。

【问题讨论】:

    标签: java spring-boot thymeleaf


    【解决方案1】:

    Thymeleaf th:each 属性的 precedence 高于 th:if 属性。所有 Thymeleaf 属性都定义了一个数字优先级,它确定了它们在标签中的执行顺序。为了满足您的要求,只需在它自己的标签中使用th:if 属性,这将是带有th:each 属性的标签的容器...

    <div th:if="${visibility}">
        <Produkt th:each="user: ${userList.myList}">
            <Name th:text="${user.name}"/>
        </Produkt>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多