Thymeleaf日期选择器:使用内置组件轻松实现日期选择功能
Thymeleaf提供了一个内置的日历选择器组件,可以方便地实现日期选择功能。
首先,在HTML页面中引入Thymeleaf命名空间:
<html xmlns:th='http://www.thymeleaf.org'>
然后,使用Thymeleaf的th:field属性绑定表单字段,并设置type属性为date:
<input type='date' th:field='*{birthDate}' />
这里的birthDate是一个Date类型的字段,它会被绑定到表单中的日期选择器上。
最后,在控制器中设置@ModelAttribute注解,以便在表单提交时获取日期值:
@GetMapping("/form")
public String showForm(Model model) {
model.addAttribute("user", new User());
return "form";
}
@PostMapping("/form")
public String processForm(@ModelAttribute User user) {
// 获取日期值
Date birthDate = user.getBirthDate();
// ...
return "result";
}
这样就可以在Thymeleaf中使用日历选择器,并在表单提交时获取日期值了。
原文地址: https://www.cveoy.top/t/topic/nsQL 著作权归作者所有。请勿转载和采集!