LoginSignup
37
41

More than 5 years have passed since last update.

Thymeleafでセレクトボックスを使う

Posted at

セレクトボックス <select> の生成

Thymeleafでは繰り返し出力をする要素に対して th:each 属性を記載します。

JSTLのforeachとは異なり、繰り返し出力を行う要素を別のタグで囲うのではなく、繰り返し出力をする要素そのものに記載しますので、HTMLのインデントで悩むことはありません。

実装例

select.html
<div class="col-md-3">
    <select id="jobId" name="jobId">
        <option th:each="job : ${jobList}" th:value="${job.id}" th:selected="${job.id == id}" th:inline="text">[[${job.name}]]</option>
    </select>
</div>

この例では、jobList は java.util.List型で、この中身のクラスは id と name フィールドを持つValueObjectです。

Thymeleafで繰り返し出力を指示しているのは、<select>ではなく、<option>です。
わかりやすいですよね d(・ω・)

37
41
1

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
37
41