【问题标题】:Thymeleaf if else [duplicate]Thymeleaf if else [重复]
【发布时间】:2021-11-25 13:01:21
【问题描述】:

我不知道如何使用 Thyeleaf 编写这种指令。 我已经用 html 制作了我的视图。

<#if updateClient??>
        <h2>Modifica del prodotto - ${updateClient.nome}</h2>
        <div style="margin: 20px;">
            <form method="POST" action="update" id="updateClient">
                <input type="hidden" name="id" value="${updateClient.id}"/>
                <div>
                    <label for="name">Name</label>
                    <input type="text" name="nome" id="nome" value="${updateClient.nome}" />
                </div>
                <div>
                    <input type="submit" name="invia" value="Update" />
                </div>
            </form>
        </div>
    <#else>
        <h2>New Client</h2>
        <div style="margin: 20px;">
            <form method="POST" action="add" id="newDataClient">
                <div>
                    <label for="nome">Nome</label>
                    <input type="text" name="nome" id="nome" value="" />
                </div>
                
                <div>
                    <input type="submit" name="invia" value="Add" />
                </div>
            </form>
        </div>
    </#if>

这是使用 FreeMarker 编写的,但我需要使用 Thymeleaf。 我知道简单的 Thymeleaf 指令,但我不知道该怎么做。我阅读了 Thymeleaf 指令,发现如何迭代变量列表,但不知道如何使用 if 和 else 创建这个结构。如果可能的话,可以创建这样的东西。

【问题讨论】:

  • @SimonMartinelli 我读过这个。但在这种情况下,他会尝试使用变量。我尝试使用添加网址和更新网址。因此,如果用户选择“添加”一个新客户端,我将看到添加表单。否则,如果用户选择“更新”一个 Ciente,我将看到更新表单。
  • @helloJava 看起来像链接副本的第二个答案那样工作,不是吗?
  • 您可以使用三元运算符:类似:th:action="${some_condition} ? '/user/create/' : '/user/update/' + ${user.id}"作为 some_condition 你可以发送一些布尔变量
  • @FedericoklezCulloca 它不起作用。我只看到连接到 url 的一段代码,所以我看不到所有视图并在那里执行所有 crud 操作。因此,如果我写了 url book/all-> 我只是看到了这个观点,而 crud 操作的其他部分不起作用,我看不到它们。但是我在视图中插入了所有用于 crud 操作的东西:添加、更新、删除......

标签: java if-statement thymeleaf


【解决方案1】:
<div th:if="${updateClient.nome}">
   <div>Edit Form</div>
</div>
<!-- ELSE -->
<div th:unless="${updateClient.nome}">
   <div>New Form</div>
</div>

【讨论】:

  • 您可以使用三元运算符:类似:th:action="${some_condition} ? '/user/create/' : '/user/update/' + ${user.id}"作为 some_condition 你可以发送一些布尔变量
  • 您的答案可以通过额外的支持信息得到改进。请编辑以添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以在help center找到更多关于如何写好答案的信息。
猜你喜欢
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 2012-11-09
  • 1970-01-01
  • 2013-09-10
  • 2012-01-10
相关资源
最近更新 更多