展示之前在Thymeleaf中使用if case隐藏的标签。

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

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?:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;th:block th:fragment=&quot;parent_fragment&quot;&gt;
    &lt;Parent name=&quot;Unknown&quot; surname=&quot;Uknown&quot;&gt;
        &lt;Birthday&gt;01.01.1990&lt;/Birthday&gt;
    &lt;/Parent&gt;
&lt;/th:block&gt;

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)

&lt;Child name=&quot;Filip&quot; surname=&quot;Trajkovski&quot;&gt;
    &lt;Birtday&gt;01.01.1999&lt;/Birtday&gt;
    &lt;th:block th:if=&quot;${subChild} != null&quot;&gt;
    &lt;SubChild name=&quot;Nikola&quot; surname=&quot;Tesla&quot;&gt;
        &lt;Birtday&gt;01.01.1990&lt;/Birtday&gt;
        &lt;th:block th:replace = &quot;parent.xml :: parent_fragment&quot;&gt;&lt;/th:block&gt;
    &lt;/SubChild&gt;        
    &lt;/th:block&gt;
    &lt;th:block th:if=&quot;${subChild} == null&quot;&gt;
        &lt;th:block th:replace = &quot;parent.xml :: parent_fragment&quot;&gt;&lt;/th:block&gt;
    &lt;/th:block&gt;
&lt;/Child&gt;

This generates two XML outputs as follows:

When subchild does not exist (or, in my case, when it is null):

&lt;Child name=&quot;Filip&quot; surname=&quot;Trajkovski&quot;&gt;
    &lt;Birtday&gt;01.01.1999&lt;/Birtday&gt;
    &lt;Parent name=&quot;Unknown&quot; surname=&quot;Uknown&quot;&gt;
        &lt;Birthday&gt;01.01.1990&lt;/Birthday&gt;
    &lt;/Parent&gt;
&lt;/Child&gt;

When subchild exists (not null):

&lt;Child name=&quot;Filip&quot; surname=&quot;Trajkovski&quot;&gt;
	&lt;Birtday&gt;01.01.1999&lt;/Birtday&gt;
	&lt;SubChild name=&quot;Nikola&quot; surname=&quot;Tesla&quot;&gt;
		&lt;Birtday&gt;01.01.1990&lt;/Birtday&gt;
		&lt;Parent name=&quot;Unknown&quot; surname=&quot;Uknown&quot;&gt;
			&lt;Birthday&gt;01.01.1990&lt;/Birthday&gt;
		&lt;/Parent&gt;
	&lt;/SubChild&gt;        
&lt;/Child&gt;

Now, you only have to define the (potentially very large) &lt;parent&gt;...&lt;/parent&gt; section in one place.

huangapple
  • 本文由 发表于 2020年5月5日 21:56:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61614865.html
匿名

发表评论

匿名网友

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

确定