-1

I don't know how to wrote this kind of insruction using Thyeleaf. I've got my view made with 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>

This was written using FreeMarker, but I need to use Thymeleaf. I know simple Thymeleaf instructions, but I don't know how to do this. I read Thymeleaf instructions, and I find how to iterate a list of variables, but not how to create this structure using if and else. And if it's possible create something like this.

helloJava
  • 45
  • 10
  • @SimonMartinelli i read this one. But in those case he try to work with variables. Me try to work with add url and update url. So if user choose "add" a new Client I will see add form. Else if User choose to "update" one Ciente, I will see update form. – helloJava Oct 05 '21 at 08:45
  • 1
    @helloJava looks like the second answer to the linked duplicate works that way, doesn't it? – Federico klez Culloca Oct 05 '21 at 08:49
  • You can use ternary operator: something like: th:action="${some_condition} ? '/user/create/' : '/user/update/' + ${user.id}" As some_condition you can send some boolean variable – nurmanbetov Oct 05 '21 at 08:50
  • @FedericoklezCulloca it doesn't work. I see just the piece of code connected to the url, so I can' see all view and do there all crud operations. So if I wrote url book/all->I se just this pieace of view, and others part of crud operations doesn't work and I don't see them. But I insert into the view all things for crud operations: add, update, delete... – helloJava Oct 05 '21 at 10:26

1 Answers1

0
<div th:if="${updateClient.nome}">
   <div>Edit Form</div>
</div>
<!-- ELSE -->
<div th:unless="${updateClient.nome}">
   <div>New Form</div>
</div>
Senthil
  • 2,156
  • 1
  • 14
  • 19
  • 1
    You can use ternary operator: something like: th:action="${some_condition} ? '/user/create/' : '/user/update/' + ${user.id}" As some_condition you can send some boolean variable – nurmanbetov Oct 05 '21 at 08:50
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the [help center](https://stackoverflow.com/help/how-to-answer). – Lizesh Shakya Oct 06 '21 at 07:55