datnguyen94
datnguyen94

Reputation: 319

How to set default value in thymeleaf th:field

I have a form and I want to set default value in the field below but it's not working.

    <span>ID User:</span>

                <input type="text"   th:value="${session.name}"  th:field="*{userid}" th:errorclass="field-error"  />

            </div>  
            <div>           
                <span class="name-in">ID Room:</span>
                <input type="text"  th:value="${id}"   th:field="*{room}" th:errorclass="field-error" />
            </div>      

I read some topic about this problem and I try to change as given below

th:attr="value = ${session.name}" 

But It's still not working. Field ID User is empty. I don't know how to solve this problem.

Upvotes: 1

Views: 7482

Answers (2)

Metroids
Metroids

Reputation: 20487

Instead of changing the html, you should instead set the value of *{userid} in your controller. That way you can keep your html the same:

// Controller
modelObject.setUserId(session.name);

// HTML
<input type="text" th:field="*{userid}" th:errorclass="field-error"  />

Upvotes: 1

Avijit Barua
Avijit Barua

Reputation: 3086

Although your question contain less information, but i think you want to put default value for all field. If you like to do so change

`<input type="text"   th:value="${session.name}"  th:field="*{userid}" th:errorclass="field-error"  />`

to

<input type="text"   name="userid" value="as your wish" th:errorclass="field-error"  />

Upvotes: 1

Related Questions