Sure, I'll guide you through creating a Thymeleaf form to save multiple cells of multiple lines of a table, where each line represents a Pizza. We'll need to create a form where users can input data for each pizza and then submit the form to save the information.
Firstly, let’s assume we have a Pizza class:
public class Pizza {
private String name;
private String size;
private double price;
// getters and setters
}
In your Spring Controller, you might have something like this to handle the form display and submission:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import java.util.ArrayList;
import java.util.List;
@Controller
public class PizzaController {
@GetMapping("/pizzas")
public String showPizzaForm(Model model) {
List<Pizza> pizzas = new ArrayList<>(); // In real scenario, you could load these from database
pizzas.add(new Pizza());
pizzas.add(new Pizza());
model.addAttribute("pizzas", pizzas);
return "pizza-form";
}
@PostMapping("/pizzas")
public String submitPizzaForm(@ModelAttribute("pizzas") List<Pizza> pizzas) {
// Handle the submitted data, e.g., save to database
for (Pizza pizza : pizzas) {
System.out.println(pizza.getName());
System.out.println(pizza.getSize());
System.out.println(pizza.getPrice());
}
return "redirect:/pizzas";
}
}
Here's the pizza-form.html using Thymeleaf:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Pizza Form</title>
</head>
<body>
<h2>Pizza Form</h2>
<form th:action="@{/pizzas}" th:object="${pizzas}" method="post">
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Size</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr th:each="pizza, iterStat : ${pizzas}">
<td>
<input type="text" th:field="*{[${iterStat.index}].name}" />
</td>
<td>
<input type="text" th:field="*{[${iterStat.index}].size}" />
</td>
<td>
<input type="number" step="0.01" th:field="*{[${iterStat.index}].price}" />
</td>
</tr>
</tbody>
</table>
<button type="submit">Save</button>
</form>
</body>
</html>
Explanation
-
Domain Model (Pizza)
- The
Pizza class represents the data we want to capture.
-
Controller:
showPizzaForm: Method to initialize and display the form. Adds a list of Pizza objects to the model.
submitPizzaForm: Method to handle form submission. Uses @ModelAttribute to bind the form data to the List<Pizza>.
-
Thymeleaf Template:
- Loop with
th:each: Iterates over the list of pizzas. iterStat provides the index, which is used to bind each input field to the corresponding pizza in the list.
- Fields with
th:field: Binds each input element to the corresponding field in the Pizza object at the current index.
This template will render a table where users can input the details for each pizza. When the form is submitted, the controller will capture the list of pizzas and can process or save them as needed.