Il est utilisé lorsque vous souhaitez générer une table avec des éléments variables à afficher par Thymeleaf.
SpringBoot 2.0.3.RELEASE ( Thymeleaf 3.0.9.RELEASE )
Depuis Controller, passez celui au format Map <String, Object>. Cependant, j'utilise LinkedHashMap parce que je veux toujours définir l'ordre de sortie de Map.
package alphapz.thymeleaf.controller;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * @author A-pZ
 *
 */
@Controller
public class MatrixController {
	@GetMapping("")
	public ModelAndView display(ModelAndView mnv) {
		mnv.addObject("data", matrix);
		mnv.setViewName("index");
		return mnv;
	}
	private Map<String, Object> matrix = new LinkedHashMap<String, Object>() {{
		put("id","ID001");
		put("name","username");
		put("address","USER_ADDRESS");
	}};
}
View (Template)
Puisque la carte précédente est stockée avec le nom des données, spécifiez-la avec th: each, qui affiche à plusieurs reprises le système de collecte.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
	<table>
		<thead class="thead-dark">
			<tr>
				<th th:each="entry : ${data}" th:text="${entry.key}"></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td th:each="entry : ${data}" th:text="${entry.value}"></td>
			</tr>
		</tbody>
	</table>
</body>
</html>
L'élément récupéré par th: each est un EntrySet, donc le nom de la clé peut être obtenu à partir de la clé et la valeur peut être obtenue à partir de la valeur.