不要在Thymeleaf中处理每个情况,当if条件为真时。

huangapple go评论76阅读模式
英文:

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:

&lt;Produkt th:if=&quot;${visibility}&quot; th:each=&quot;user: ${userList.myList}&quot;&gt;
    &lt;Name th:text=&quot;${user.name}&quot;/&gt;
&lt;/Produkt&gt;

I have the following java code:

@Test
public void myTest() {
    Context context = new Context();
    Map&lt;String, List&lt;Map&lt;String, String&gt;&gt;&gt; userValuesList = new HashMap&lt;&gt;();
    boolean visibility = getVisibility();
    if (visibility) {
        List&lt;Map&lt;String, String&gt;&gt; vv = new ArrayList&lt;&gt;();
        Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
        map.put(&quot;name&quot;, &quot;filip&quot;);
        vv.add(map);
        Map&lt;String, String&gt; map2 = new HashMap&lt;&gt;();
        map2.put(&quot;name&quot;, &quot;test&quot;);
        vv.add(map2);
        userValuesList.put(&quot;myList&quot;, vv);
    }
    context.setVariable(&quot;visibility&quot;, visibility);
    context.setVariable(&quot;userList&quot;, userValuesList);
    springTemplateEngine.process(&quot;test.xml&quot;, 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:

&lt;Produkt&gt;
    &lt;Name&gt;filip&lt;/Name&gt;
&lt;/Produkt&gt;

&lt;Produkt&gt;
    &lt;Name&gt;test&lt;/Name&gt;
&lt;/Produkt&gt;

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&#39;s own tag which will be a container to the tag with `th:each` attribute ...

    &lt;div th:if=&quot;${visibility}&quot;&gt;
        &lt;Produkt th:each=&quot;user: ${userList.myList}&quot;&gt;
            &lt;Name th:text=&quot;${user.name}&quot;/&gt;
        &lt;/Produkt&gt;
    &lt;/div&gt;

  [1]: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#attribute-precedence

</details>



huangapple
  • 本文由 发表于 2020年4月6日 22:06:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定