liveBook · Manning

Th:value

As explained in Spring in Action, Sixth Edition

th:value is a Thymeleaf namespace attribute that sets an output element's value from an expression.

th:value sets a rendered HTML element’s value from a Thymeleaf expression. In a taco design form, it assigns each ingredient checkbox the ingredient’s id. section 2.1.3

Ingredient checkbox

Use th:value="${ingredient.id}" inside a th:each loop. Each rendered checkbox then carries the current ingredient’s identifier, while th:field binds the checkbox group to the form object. section 2.1.3

<h3>Designate your wrap:</h3>
<div th:each="ingredient : ${wrap}">
  <input th:field="*{ingredients}" type="checkbox" 
         th:value="${ingredient.id}"/>
  <span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>

A complete snippet combines th:each, th:field, and th:value to render selectable wrap ingredients from model data. section 2.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains rendered output and form interaction, but does not detail Thymeleaf expression evaluation internals.
Examples
Shows ingredient checkboxes whose values come from ingredient.id while labels display ingredient.name.
What it assumes
Assumes basic HTML forms and Thymeleaf expressions while demonstrating collection iteration and model-backed fields.
What you can do after
Build a data-driven checkbox form that submits ingredient identifiers with Thymeleaf.
How it connects
Connects th:value with th:each, th:field, HTML forms, validation, and model attributes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.