【发布时间】: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