How to work with dates in Thymeleaf (#960)

* How to work with dates in Thymeleaf

* Fixes in PR for Thymeleaf
This commit is contained in:
maibin 2017-01-08 16:01:41 +01:00 committed by KevinGilmore
parent 82fc8cf8fc
commit e36d928219
6 changed files with 94 additions and 18 deletions

1
spring-thymeleaf/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target/

View File

@ -15,7 +15,8 @@
<org.slf4j.version>1.7.21</org.slf4j.version> <org.slf4j.version>1.7.21</org.slf4j.version>
<logback.version>1.1.7</logback.version> <logback.version>1.1.7</logback.version>
<!-- thymeleaf --> <!-- thymeleaf -->
<org.thymeleaf-version>3.0.2.RELEASE</org.thymeleaf-version> <org.thymeleaf-version>3.0.3.RELEASE</org.thymeleaf-version>
<org.thymeleaf.extras-version>3.0.0.RELEASE</org.thymeleaf.extras-version>
<thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version> <thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
<!-- validation --> <!-- validation -->
<javax.validation-version>1.1.0.Final</javax.validation-version> <javax.validation-version>1.1.0.Final</javax.validation-version>
@ -78,6 +79,11 @@
<artifactId>thymeleaf-layout-dialect</artifactId> <artifactId>thymeleaf-layout-dialect</artifactId>
<version>${thymeleaf-layout-dialect.version}</version> <version>${thymeleaf-layout-dialect.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>${org.thymeleaf.extras-version}</version>
</dependency>
<!-- Logging --> <!-- Logging -->
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>

View File

@ -13,6 +13,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine; import org.thymeleaf.TemplateEngine;
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.spring4.view.ThymeleafViewResolver;
@ -74,6 +75,7 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application
private TemplateEngine templateEngine(ITemplateResolver templateResolver) { private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine engine = new SpringTemplateEngine(); SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new LayoutDialect(new GroupingStrategy())); engine.addDialect(new LayoutDialect(new GroupingStrategy()));
engine.addDialect(new Java8TimeDialect());
engine.setTemplateResolver(templateResolver); engine.setTemplateResolver(templateResolver);
return engine; return engine;
} }

View File

@ -0,0 +1,25 @@
package com.baeldung.thymeleaf.controller;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class DatesController {
@RequestMapping(value = "/dates", method = RequestMethod.GET)
public String getInfo(Model model) {
model.addAttribute("standardDate", new Date());
model.addAttribute("localDateTime", LocalDateTime.now());
model.addAttribute("localDate", LocalDate.now());
model.addAttribute("timestamp", Instant.now());
return "dates.html";
}
}

View File

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Baeldung - dates</title>
</head>
<body>
<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>
<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>
<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>
</body>
</html>

View File

@ -51,8 +51,15 @@ public class ExpressionUtilityObjectsControllerIntegrationTest {
} }
@Test @Test
public void testGetDates() throws Exception{ public void testGetObjects() throws Exception {
mockMvc.perform(get("/objects").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("objects.html")); mockMvc.perform(get("/objects").with(testUser()).with(csrf())).andExpect(status().isOk())
.andExpect(view().name("objects.html"));
}
@Test
public void testDates() throws Exception {
mockMvc.perform(get("/dates").with(testUser()).with(csrf())).andExpect(status().isOk())
.andExpect(view().name("dates.html"));
} }
} }