セレクトボックスで生年月日を作る
ちょっと時間かかったので備忘録として残しておきます。
#numbers.sequenceで選択できる範囲を指定するのがポイント。
index.html
<div>
<select id="birtyday" name="birthday">
<option th:each="i : ${#numbers.sequence(2020, 1920)}"
th:value="${i}" th:text="${i}" th:selected="${i == birthYear}">
</option>
</select>
<select id="birthMonth" name="birthMonth">
<option th:each="i : ${#numbers.sequence(1, 12)}" th:value="${i}"
th:text="${i}" th:selected="${i == birthMonth}"></option>
</select>
<select id="birthDay" name="birthDay">
<option th:each="i : ${#numbers.sequence(1, 31)}" th:value="${i}"
th:text="${i}" th:selected="${i == birthDay}"></option>
</select>
</div>
Birth.java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Birth {
@GetMapping("/index")
public String getIndex(Model model) {
int birthYear = 2002;
int birthMonth = 8;
int birthDay = 3;
model.addAttribute("birthYear", birthYear);
model.addAttribute("birthMonth", birthMonth);
model.addAttribute("birthDay", birthDay);
return "index";
}
}