4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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タグに「/」が入っているかどうかということだけ。
意外と見逃してしまいますね。。。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?