I have a simple text input field where i have to set default value from one object and save its final value in other. The following code is not working.
<div th:object="${form}"> <input class="form-control" type="text" th:value="${client.name}" //this line is ignored th:field="*{clientName}"/> </div>
form is DTO object and client is Entity object from database.
What is the correct way to solve this situation?
By not working I mean - lets say that initial values are client.name="Foo" and form.clientName=null. I need that input field display value is "Foo" and after form submission form.clientName value to become "Foo". But the input field displays nothing and on submission form.clientName value still is null;
If anyone is interested, solved this issue using following structure (found the answer in another question).
th:attr="value = ${client.name}"
Define Variables There are two ways we can define a variable in a Thymeleaf template. The first option is to take a single element while iterating over an array: As a result, we'll get a <div> with several <a> elements corresponding to the number of articles in the articles variable. Another way is to define a new variable based on another one.
The method annotated with @GetMapping will take a single parameter – the Model. It holds all the global variables that can be further used inside the Thymeleaf template. In our case, the model will have just one parameter – the list of articles. The Article class will consist of two String fields, name and url:
In this case, if $ {condition} is false, null will be returned, and thus no removal will be performed. Thymeleaf calls local variables those variables that are defined for a specific fragment of a template, and are only available for evaluation inside that fragment. An example we have already seen is the prod iter variable in our product list page:
Thymeleaf is a Java library. It is an XML/XHTML/HTML5 template engine able to apply a set of transformations to template files in order to display data and/or text produced by your applications. It is better suited for serving XHTML/HTML5 in web applications, but it can process any XML file, be it in web or in standalone applications.
You could approach this method.
Instead of using th:field
use html id
& name
. Set value using th:value
<input class="form-control" type="text" th:value="${client.name}" id="clientName" name="clientName" />
Hope this will help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With