英文:
Show tag previously hidden with if case in thymeleaf
问题
我有以下的 XML:
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<SubChild th:if="${subChild.exist}" name="Nikola" surname="Tesla">
<Birtday>01.01.1990</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</SubChild>
</Child>
我正在使用 Java 从数据中填充这个标签,在某些情况下,我没有这个 SubChild,对于它我有一个 if 条件,但我总是有一个父标签,我需要显示它。
我想要实现的是,即使在 SubChild 不存在并且通过 if 条件隐藏的情况下,也能显示 Parent 标签,因为它总是存在的。为了使这个 Parent 标签变成必须的。
我有一个解决方案是:
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<SubChild th:if="${subChild.exist}" name="Nikola" surname="Tesla">
<Birtday>01.01.1990</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</SubChild>
<Parent th:if="${!subChild.exist}" name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</Child>
但在这个解决方案中,Parent 标签重复了两次。在我真实的场景中,这是一个很大的标签,我希望避免重复它。我的问题是:是否可能在之前提到的 if 条件为 false 的情况下显示这个标签,而不重复相同的 Parent 代码。
最终我的结果应该是:
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</Child>
英文:
I have the following xml:
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<SubChild th:if="${subChild.exist}" name="Nikola" surname="Tesla">
<Birtday>01.01.1990</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</SubChild>
</Child>
I am populating this tag with data from java and in some cases I don't have this SubChild for which I have that if case, but I always have the parent which I need to show it.
What I want to achieve is even in a case where this SubChild doesn't exist and it's hidden with the if case, to show the Parent tag since he will always be there. To make this tag Parent mandatory I would say.
I have one solution which is:
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<SubChild th:if="${subChild.exist}" name="Nikola" surname="Tesla">
<Birtday>01.01.1990</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</SubChild>
<Parent th:if="${!subChild.exist}" name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</Child>
But in this solution is repetition of the Parent tag twice. And in my real scenario it is a huge tag which I want to avoid repeating it.
My question is: is it possible to display this tag even if the previously mentioned if case is false, without repeating the same code for the Parent.
In the end my result should be:
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</Child>
答案1
得分: 2
使用片段,您可以执行以下操作:
为您的 XML 中的“父”部分创建一个片段。在我的示例中,这在一个名为“parent.xml”的文件中:
<?xml version="1.0" encoding="UTF-8"?>
<th:block th:fragment="parent_fragment">
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</th:block>
然后在您的主 XML 中如下使用该片段:
(仅供参考,为了进行我的测试,我更改了您的 if
条件 - 您可以将其改回您的版本)
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<th:block th:if="${subChild} != null">
<SubChild name="Nikola" surname="Tesla">
<Birtday>01.01.1990</Birtday>
<th:block th:replace="parent.xml :: parent_fragment"></th:block>
</SubChild>
</th:block>
<th:block th:if="${subChild} == null">
<th:block th:replace="parent.xml :: parent_fragment"></th:block>
</th:block>
</Child>
这将生成两个 XML 输出,如下所示:
当 subchild 不存在(或者在我的情况下为 null)时:
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</Child>
当 subchild 存在(非 null)时:
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<SubChild name="Nikola" surname="Tesla">
<Birtday>01.01.1990</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</SubChild>
</Child>
现在,您只需要在一个地方定义(可能非常庞大的)<parent>...</parent>
部分。
英文:
Using fragments, you can do the following:
Create a fragment for the "parent" section of your XML. In my example this is in a file called "parent.xml?:
<?xml version="1.0" encoding="UTF-8"?>
<th:block th:fragment="parent_fragment">
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</th:block>
Then use the fragment in your main XML as follows:
(just to note, for my own testing, I changed your if
condition - you can change it back to your version)
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<th:block th:if="${subChild} != null">
<SubChild name="Nikola" surname="Tesla">
<Birtday>01.01.1990</Birtday>
<th:block th:replace = "parent.xml :: parent_fragment"></th:block>
</SubChild>
</th:block>
<th:block th:if="${subChild} == null">
<th:block th:replace = "parent.xml :: parent_fragment"></th:block>
</th:block>
</Child>
This generates two XML outputs as follows:
When subchild does not exist (or, in my case, when it is null):
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</Child>
When subchild exists (not null):
<Child name="Filip" surname="Trajkovski">
<Birtday>01.01.1999</Birtday>
<SubChild name="Nikola" surname="Tesla">
<Birtday>01.01.1990</Birtday>
<Parent name="Unknown" surname="Uknown">
<Birthday>01.01.1990</Birthday>
</Parent>
</SubChild>
</Child>
Now, you only have to define the (potentially very large) <parent>...</parent>
section in one place.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论