英文:
Don't process the each case when if case is true in thymeleaf
问题
以下是翻译好的内容:
我正在开发一个 Spring Boot 应用程序。我有以下 Thymeleaf 块:
    <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);
    }
因为我只在 visibility 为 true 时添加这个 myList,我得到了以下错误:
**在评估 SpringEL 表达式时出现异常:"userList.myList"**
我的问题是,是否可能仅在 visibility 为 true 时由 springTemplateEngine 处理这个 userList.myList?
我想要实现的最终结果是:
    <Produkt>
        <Name>filip</Name>
    </Produkt>
    <Produkt>
        <Name>test</Name>
    </Produkt>
如果 visibility 为 false,在这种情况下,整个 Produkt 标签都会消失。
英文:
I am working on a spring boot application. I have the following thymeleaf block:
<Produkt th:if="${visibility}" th:each="user: ${userList.myList}">
    <Name th:text="${user.name}"/>
</Produkt>
I have the following java code:
@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);
}
Since I am adding this myList only when visibility is true, I am getting the following error:
Exception evaluating SpringEL expression: "userList.myList"
My question is is it possible for this userList.myList to be processed by the springTemplateEngine only if visibility is true ?
The end result that I want to achieve is:
<Produkt>
    <Name>filip</Name>
</Produkt>
<Produkt>
    <Name>test</Name>
</Produkt>
and in case if visbility is false, this whole Produkt tag to be gone.
答案1
得分: 1
Thymeleaf的`th:each`属性的优先级高于`th:if`属性。所有Thymeleaf属性都定义了一个数值优先级,该优先级确定了它们在标签中执行的顺序。要实现您的要求,只需在其自己的标签中使用`th:if`属性,该标签将作为包含`th:each`属性的标记的容器...
    <div th:if="${visibility}">
        <Produkt th:each="user: ${userList.myList}">
            <Name th:text="${user.name}"/>
        </Produkt>
    </div>
<details>
<summary>英文:</summary>
Thymeleaf `th:each` attribute has higher [precedence][1] than `th:if` attribute. All Thymeleaf attributes define a numeric precedence, which establishes the order in which they are executed in the tag. To achieve your requirements just use `th:if` attribute in it's own tag which will be a container to the tag with `th:each` attribute ...
    <div th:if="${visibility}">
        <Produkt th:each="user: ${userList.myList}">
            <Name th:text="${user.name}"/>
        </Produkt>
    </div>
  [1]: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#attribute-precedence
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论