LoginSignup
1
1

More than 3 years have passed since last update.

Thymeleafのセレクトボックスで生年月日を作る

Last updated at Posted at 2020-12-23

セレクトボックスで生年月日を作る

image.png

ちょっと時間かかったので備忘録として残しておきます。

#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";
    }
}
1
1
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
1
1