[thymeleaf]each内蔵チェックボックス/radio btn/select例


Checkbox

@ModelAttribute("regions")
    public Map<String, String> regions() {
        Map<String, String> regions = new LinkedHashMap<>();
        regions.put("SEOUL","서울");
        regions.put("BUSAN","부산");
        regions.put("JEJU","제주");
        return regions;
    }
<form th:object="${item}">
...
<div th:each="region : ${regions}" class="form-check form-check-inline">
	<input type="checkbox" th:field="*{regions}" th:value="${region.key}" class="form-check-input" disabled>
	<label th:for="${#ids.prev('regions')}"
		   th:text="${region.value}" class="form-check-label">서울</label>
</div>
th:field=「*{領域}」には、選択した値が含まれます.
th:value=「${region.key}」には、各チェックボックスのキー値が含まれます.
各キー値が選択した値と比較された場合、アイテムがONの場合はnullとして処理されます.
いちいちチェックするはずだったのに、この時間帯は自分で見てやった.

Radio Button

public enum ItemType {
    BOOK("도서"), FOOD("식품"), ETC("기타");
    private final String description;

    ItemType(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}
<div>
	<div>상품 종류</div>
	<div th:each="type : ${itemTypes}" class="form-check form-check-inline">
		<input type="radio" th:field="*{itemType}" th:value="${type.name()}" class="form-check-input" disabled>
		<label th:for="${#ids.prev('itemType')}" th:text="${type.description}" class="form-check-label">
        </label>
	</div>
</div>
Enumオブジェクトが使用されています
1.${type.description}getterが必要
2. type.name()enumの名前を取得します.(BOOK, FOOD, ETC)

Select

<div>
	<div>배송 방식</div>
		<select th:field="*{deliveryCode}" class="form-select">
		<option value="">==배송 방식 선택==</option>
		<option th:each="deliveryCode : ${deliveryCodes}" th:value="${deliveryCode.code}"
                th:text="${deliveryCode.displayName}">FAST</option>
	</select>
</div>