在 Thymeleaf 中,可以使用 HTML5 的 input 标签的 type 属性为 'date' 来实现时间选择器。具体方法如下:

  1. 在 html 页面中引入 Thymeleaf 的命名空间
<html xmlns:th="http://www.thymeleaf.org">
  1. 在表单中使用 input 标签,并设置 type 为 'date'
<form>
    <label for="date">选择日期:</label>
    <input type="date" id="date" name="date" th:value="${date}" />
</form>
  1. 在控制器中设置日期类型的值
@GetMapping("/date")
public String date(Model model) {
    LocalDate currentDate = LocalDate.now();
    model.addAttribute("date", currentDate);
    return "date";
}

这样就可以在页面中显示当前日期,并且可以选择日期。选择的日期会被传递到控制器中进行处理。

Thymeleaf 日期选择器:使用 HTML5 input type='date' 实现

原文地址: https://www.cveoy.top/t/topic/nsPL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录