1。介绍

Thymeleaf 是一个 Java 模板引擎,可能间接与 Spring 一起工作。无关 Thymeleaf 和 Spring 的介绍,请查看 this write-up。

除了这些基本功能之外,Thymeleaf 还为咱们提供了一组实用程序对象,它们将帮忙咱们在应用程序中执行常见工作。

在本教程中,咱们将探讨新旧 Java Date 类的解决和格式化以及 Thymeleaf 3.0 的一些个性。

2。 Maven 依赖项

首先,让咱们创立配置以将 Thymeleaf 与 Spring 集成到咱们的 pom.xml 中:

<dependency>    <groupId>org.thymeleaf</groupId>    <artifactId>thymeleaf</artifactId>    <version>3.0.11.RELEASE</version></dependency><dependency>    <groupId>org.thymeleaf</groupId>    <artifactId>thymeleaf-spring5</artifactId>    <version>3.0.11.RELEASE</version></dependency>

最新版本的 thymeleafthymeleaf-spring5能够能够在 Maven 核心找到。请留神,对于 Spring 4 我的项目,必须应用 thymeleaf-spring4 库而不是 thymeleaf-spring5

此外,为了应用新的 Java 8 Date 类,咱们须要向 pom.xml 增加另一个依赖项:

<dependency>    <groupId>org.thymeleaf.extras</groupId>    <artifactId>thymeleaf-extras-java8time</artifactId>    <version>3.0.4.RELEASE</version></dependency>

thymeleaf extras 是一个可选模块,由官网 Thymeleaf 团队反对,这是为了与 Java 8 Time API 兼容而创立的。它在表达式求值期间将 #temporals 对象增加到 Context 作为实用对象处理器。这意味着它能够用于评估对象图形导航语言 (OGNL) 和 Spring 表达式语言 (SpringEL) 中的表达式。

3。新旧:java.utiljava.time

Time 包是 Java SE 平台的新日期、工夫和日历 API。此新 API 与旧的旧 Date API 之间的次要区别在于,新 API 辨别了时间轴的机器视图和人类视图。机器视图显示绝对于 epoch 的一系列整数值,而人类视图显示一组字段(例如,年、月和日)。

要应用新的 Time 包,咱们须要配置咱们的模板引擎以应用新的 Java8TimeDialect

private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {    SpringTemplateEngine engine = new SpringTemplateEngine();    engine.addDialect(new Java8TimeDialect());    engine.setTemplateResolver(templateResolver);    return engine;}

这将增加相似于规范方言中的 #temporals 对象,容许从 Thymeleaf 模板格式化和创立 Temporal 对象。

为了测试新旧类的解决,咱们将创立以下变量并将它们作为模型对象增加到咱们的控制器类中:

model.addAttribute("standardDate", new Date());model.addAttribute("localDateTime", LocalDateTime.now());model.addAttribute("localDate", LocalDate.now());model.addAttribute("timestamp", Instant.now());

当初,咱们筹备好应用 Thymeleaf 的 ExpressionTemporals 实用程序对象了。

3.1。格式化日期

咱们要介绍的第一个函数是格式化 Date 对象(增加到 Spring 模型参数中)。咱们将应用 ISO8601 格局:

<h1>Format ISO</h1><p th:text="${#dates.formatISO(standardDate)}"></p><p th:text="${#temporals.formatISO(localDateTime)}"></p><p th:text="${#temporals.formatISO(localDate)}"></p><p th:text="${#temporals.formatISO(timestamp)}"></p>

无论咱们的 Date 在后端如何设置,它都会依据所选规范显示在 Thymeleaf 中。 standardDate 将由#dates 实用程序解决。新的 LocalDateTimeLocalDateInstant 类将由 #temporals 实用程序解决。

此外,如果咱们想手动设置格局,咱们能够应用:

<h1>Format manually</h1><p th:text="${#dates.format(standardDate, 'dd-MM-yyyy HH:mm')}"></p><p th:text="${#temporals.format(localDateTime, 'dd-MM-yyyy HH:mm')}"></p><p th:text="${#temporals.format(localDate, 'MM-yyyy')}"></p>

正如咱们所察看到的,咱们无奈应用 #temporals.format(...) 解决 Instant 类 --- 这将导致 UnsupportedTemporalTypeException 。此外,仅当咱们仅指定特定日期字段,跳过工夫字段时,才可能格式化 LocalDate

让咱们看看最终后果:

3.2。获取特定日期字段

为了获取 java.util.Date 类的特定字段,咱们应该应用以下实用程序对象:

${#dates.day(date)}${#dates.month(date)}${#dates.monthName(date)}${#dates.monthNameShort(date)}${#dates.year(date)}${#dates.dayOfWeek(date)}${#dates.dayOfWeekName(date)}${#dates.dayOfWeekNameShort(date)}${#dates.hour(date)}${#dates.minute(date)}${#dates.second(date)}${#dates.millisecond(date)}

对于新的 java.time 包,咱们应该保持应用 #temporals 实用程序:

${#temporals.day(date)}${#temporals.month(date)}${#temporals.monthName(date)}${#temporals.monthNameShort(date)}${#temporals.year(date)}${#temporals.dayOfWeek(date)}${#temporals.dayOfWeekName(date)}${#temporals.dayOfWeekNameShort(date)}${#temporals.hour(date)}${#temporals.minute(date)}${#temporals.second(date)}${#temporals.millisecond(date)}

让咱们看几个例子。首先,让咱们显示一周中的明天:

<h1>Show only which day of a week</h1><p th:text="${#dates.day(standardDate)}"></p><p th:text="${#temporals.day(localDateTime)}"></p><p th:text="${#temporals.day(localDate)}"></p>

接下来,让咱们显示工作日的名称:

<h1>Show the name of the week day</h1><p th:text="${#dates.dayOfWeekName(standardDate)}"></p><p th:text="${#temporals.dayOfWeekName(localDateTime)}"></p><p th:text="${#temporals.dayOfWeekName(localDate)}"></p>

最初,让咱们显示当天的以后秒数:

<h1>Show the second of the day</h1><p th:text="${#dates.second(standardDate)}"></p><p th:text="${#temporals.second(localDateTime)}"></p>

请留神,为了应用工夫局部,咱们须要应用 LocalDateTime ,因为 LocalDate 会引发谬误。

4。如何在表单中应用日期选择器

让咱们看看如何应用日期选择器从 Thymeleaf 表单提交 Date

首先,让咱们创立一个带有 Date 属性的 Student 类:

public class Student implements Serializable {    @DateTimeFormat(pattern = "yyyy-MM-dd")    private Date birthDate;}

@DateTimeFormat 正文申明 birthDate 字段应格式化为 a Date

接下来,咱们将创立一个 Thymeleaf 表单来提交 Date 输出:

<form th:action="@{/saveStudent}" method="post" th:object="${student}">    <div>        <label for="student-birth-date">Date of birth:</label>        <input type="date" th:field="${student.birthDate}" id="student-birth-date"/>    </div>    <div>        <button type="submit" class="button">Submit</button>    </div></form>

当咱们提交表单时,控制器将截获在表单中映射的带有 th:object 属性的 Student 对象。此外,th:field 属性将输出值与 birthDate 字段绑定。

当初,让咱们创立一个控制器来拦挡 POST 申请:

@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)public String saveStudent(Model model, @ModelAttribute("student") Student student) {    model.addAttribute("student", student);    return "datePicker/displayDate.html";}

提交表单后,咱们将在另一个页面上显示 birthDate 值,格局为 dd/MM/yyyy

<h1>Student birth date</h1><p th:text="${#dates.format(student.birthDate, 'dd/MM/yyyy')}"></p>

结果显示了带有日期选择器的表单:

提交表单后,咱们将看到选定的日期:

5。小结

在本教程中,咱们探讨了 Thymeleaf 框架 3.0 版中实现的 Java Date 解决性能。

如何测试? 咱们的倡议是先在浏览器中应用代码,而后再查看咱们现有的 JUnit 测试。

请留神,咱们的示例并未涵盖 Thymeleaf 中的所有可用选项。如果您想理解所有类型的实用程序,请查看咱们对于 Spring 和 Thymeleaf 表达式 的文章。

本教程的残缺实现能够在 [over on GitHub](https://github.com/eugenp/tut... "The Full Registration/Authentication Example Project on Github ”)。
本文亦通过 NoOne 的集体博客 发表。