liveBook · Manning

Web form

As explained in Spring in Action, Sixth Edition

An HTML form is a browser document structure containing controls for submitting user-entered data to a server.

A web form is an HTML structure that gathers user-entered values through controls and submits them to a server. In a taco designer, it collects ingredient choices and the taco’s name. section 2.1.3

A Thymeleaf form binds controls to an object with th:object and connects checkbox values through th:field. Iteration renders one control for each available ingredient, while validation support can preserve checked state when the form is redisplayed. section 2.1.3

Taco design form

A complete page uses a POST form, groups wrap and protein choices, renders ingredient names, includes a submit button and name field, and displays at /design when the application runs. section 2.1.3

Listing 2.5 The complete design-a-taco page
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Taco Cloud</title>
    <link rel="stylesheet" th:href="@{/styles.css}" />
  </head>
 
  <body>
    <h1>Design your taco!</h1>
    <img th:src="@{/images/TacoCloud.png}"/>
 
    <form method="POST" th:object="${taco}">
    <div class="grid">
      <div class="ingredient-group" id="wraps">
      <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>
      </div>
 
      <div class="ingredient-group" id="proteins">
      <h3>Pick your protein:</h3>
      <div th:each="ingredient : ${protein}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
 
      <div class="ingredient-group" id="cheeses">
      <h3>Choose your cheese:</h3>
      <div th:each="ingredient : ${cheese}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
 
      <div class="ingredient-group" id="veggies">
      <h3>Determine your veggies:</h3>
      <div th:each="ingredient : ${veggies}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
 
      <div class="ingredient-group" id="sauces">
      <h3>Select your sauce:</h3>
      <div th:each="ingredient : ${sauce}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
      </div>
 
      <div>
 
 
      <h3>Name your taco creation:</h3>
      <input type="text" th:field="*{name}"/>
      <br/>
 
      <button>Submit Your Taco</button>
      </div>
    </form>
  </body>
</html>

Inspect the complete Thymeleaf template, including form binding, ingredient loops, static assets, and grouped checkbox controls. section 2.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Builds a complete Thymeleaf template with binding and iteration, while deferring validation implementation.
Examples
Uses a taco page with wrap and protein checkboxes, ingredient labels, a name field, a submit button, and rendered static assets.
What you can do after
Builds and runs a Thymeleaf form, then views the rendered page at /design.
How it connects
Connects HTML forms with Thymeleaf, Spring Boot, controllers, views, and validation.

Also explained in

Compare how 12 books explain web form →

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.