diff --git a/spring-thymeleaf/.gitignore b/spring-thymeleaf/.gitignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/spring-thymeleaf/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml index b387539aa1..c43ab4dbb2 100644 --- a/spring-thymeleaf/pom.xml +++ b/spring-thymeleaf/pom.xml @@ -15,14 +15,15 @@ 1.7.21 1.1.7 - 3.0.2.RELEASE + 3.0.3.RELEASE + 3.0.0.RELEASE 2.1.2 1.1.0.Final 5.3.3.Final 5.2.5.Final - 4.12 + 4.12 3.6.0 2.6 @@ -78,6 +79,11 @@ thymeleaf-layout-dialect ${thymeleaf-layout-dialect.version} + + org.thymeleaf.extras + thymeleaf-extras-java8time + ${org.thymeleaf.extras-version} + org.slf4j diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java index ab048bdd87..3dc84ed6e9 100644 --- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java @@ -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.WebMvcConfigurerAdapter; import org.thymeleaf.TemplateEngine; +import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect; import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring4.view.ThymeleafViewResolver; @@ -74,6 +75,7 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application private TemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine engine = new SpringTemplateEngine(); engine.addDialect(new LayoutDialect(new GroupingStrategy())); + engine.addDialect(new Java8TimeDialect()); engine.setTemplateResolver(templateResolver); return engine; } diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/DatesController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/DatesController.java new file mode 100644 index 0000000000..74e2356076 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/DatesController.java @@ -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"; + } + +} diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/dates.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/dates.html new file mode 100644 index 0000000000..f8db1030f2 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/dates.html @@ -0,0 +1,35 @@ + + + + +Baeldung - dates + + +

Format ISO

+

+

+

+

+ +

Format manually

+

+

+

+ +

Show only which day of a week

+

+

+

+ +

Show the name of the week day

+

+

+

+ +

Show the second of the day

+

+

+ + + \ No newline at end of file diff --git a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java index 0638dbbc11..923d98324b 100644 --- a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java +++ b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java @@ -30,29 +30,36 @@ import com.baeldung.thymeleaf.config.WebMVCSecurity; @WebAppConfiguration @ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) public class ExpressionUtilityObjectsControllerIntegrationTest { - + @Autowired - WebApplicationContext wac; - @Autowired - MockHttpSession session; + WebApplicationContext wac; + @Autowired + MockHttpSession session; - private MockMvc mockMvc; + private MockMvc mockMvc; - @Autowired - private Filter springSecurityFilterChain; + @Autowired + private Filter springSecurityFilterChain; - protected RequestPostProcessor testUser() { - return user("user1").password("user1Pass").roles("USER"); - } + protected RequestPostProcessor testUser() { + return user("user1").password("user1Pass").roles("USER"); + } - @Before - public void setup() { - mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build(); - } + @Before + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build(); + } @Test - public void testGetDates() throws Exception{ - mockMvc.perform(get("/objects").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("objects.html")); + public void testGetObjects() throws Exception { + 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")); } }