thymeleafでドロップダウンリストを実装していると、なぜか初期値が設定できないことがあります。
※前提条件:selectタグで「th:field」を使用している場合
初期値が設定されない例
sample.html
<select th:field="*{item1}">
<option
th:each="t : ${itemList1}"
th:value="${t.key}"
th:text="${t.value}"
th:selected="${t.key == selectedValue1}">
</option>
</select>
初期値が設定される例
sample.html
<select th:field="*{item1}"/>
<option
th:each="t : ${itemList1}"
th:value="${t.key}"
th:text="${t.value}"
th:selected="${t.key == selectedValue1}">
</option>
</select>
どこが違うのかというと、1行目のselectタグに「/」が入っているかどうかということだけ。
意外と見逃してしまいますね。。。