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

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

Don't process the each case when if case is true in thymeleaf

问题

以下是翻译好的内容:

  1. 我正在开发一个 Spring Boot 应用程序。我有以下 Thymeleaf 块:
  2. <Produkt th:if="${visibility}" th:each="user: ${userList.myList}">
  3. <Name th:text="${user.name}"/>
  4. </Produkt>
  5. 我有以下 Java 代码:
  6. @Test
  7. public void myTest() {
  8. Context context = new Context();
  9. Map<String, List<Map<String, String>>> userValuesList = new HashMap<>();
  10. boolean visibility = getVisibility();
  11. if (visibility) {
  12. List<Map<String, String>> vv = new ArrayList<>();
  13. Map<String, String> map = new HashMap<>();
  14. map.put("name", "filip");
  15. vv.add(map);
  16. Map<String, String> map2 = new HashMap<>();
  17. map2.put("name", "test");
  18. vv.add(map2);
  19. userValuesList.put("myList", vv);
  20. }
  21. context.setVariable("visibility", visibility);
  22. context.setVariable("userList", userValuesList);
  23. springTemplateEngine.process("test.xml", context);
  24. }
  25. 因为我只在 visibility true 时添加这个 myList,我得到了以下错误:
  26. **在评估 SpringEL 表达式时出现异常:"userList.myList"**
  27. 我的问题是,是否可能仅在 visibility true 时由 springTemplateEngine 处理这个 userList.myList
  28. 我想要实现的最终结果是:
  29. <Produkt>
  30. <Name>filip</Name>
  31. </Produkt>
  32. <Produkt>
  33. <Name>test</Name>
  34. </Produkt>
  35. 如果 visibility false,在这种情况下,整个 Produkt 标签都会消失。
英文:

I am working on a spring boot application. I have the following thymeleaf block:

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

I have the following java code:

  1. @Test
  2. public void myTest() {
  3. Context context = new Context();
  4. Map&lt;String, List&lt;Map&lt;String, String&gt;&gt;&gt; userValuesList = new HashMap&lt;&gt;();
  5. boolean visibility = getVisibility();
  6. if (visibility) {
  7. List&lt;Map&lt;String, String&gt;&gt; vv = new ArrayList&lt;&gt;();
  8. Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
  9. map.put(&quot;name&quot;, &quot;filip&quot;);
  10. vv.add(map);
  11. Map&lt;String, String&gt; map2 = new HashMap&lt;&gt;();
  12. map2.put(&quot;name&quot;, &quot;test&quot;);
  13. vv.add(map2);
  14. userValuesList.put(&quot;myList&quot;, vv);
  15. }
  16. context.setVariable(&quot;visibility&quot;, visibility);
  17. context.setVariable(&quot;userList&quot;, userValuesList);
  18. springTemplateEngine.process(&quot;test.xml&quot;, context);
  19. }

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:

  1. &lt;Produkt&gt;
  2. &lt;Name&gt;filip&lt;/Name&gt;
  3. &lt;/Produkt&gt;
  4. &lt;Produkt&gt;
  5. &lt;Name&gt;test&lt;/Name&gt;
  6. &lt;/Produkt&gt;

and in case if visbility is false, this whole Produkt tag to be gone.

答案1

得分: 1

  1. Thymeleaf`th:each`属性的优先级高于`th:if`属性。所有Thymeleaf属性都定义了一个数值优先级,该优先级确定了它们在标签中执行的顺序。要实现您的要求,只需在其自己的标签中使用`th:if`属性,该标签将作为包含`th:each`属性的标记的容器...
  2. <div th:if="${visibility}">
  3. <Produkt th:each="user: ${userList.myList}">
  4. <Name th:text="${user.name}"/>
  5. </Produkt>
  6. </div>
  1. <details>
  2. <summary>英文:</summary>
  3. 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 ...
  4. &lt;div th:if=&quot;${visibility}&quot;&gt;
  5. &lt;Produkt th:each=&quot;user: ${userList.myList}&quot;&gt;
  6. &lt;Name th:text=&quot;${user.name}&quot;/&gt;
  7. &lt;/Produkt&gt;
  8. &lt;/div&gt;
  9. [1]: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#attribute-precedence
  10. </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:

确定