I am using spring boot 2.2.4 and using the thymealeaf text templates for the first time with spring boot.
Belowis the texttemplate I am trying to use and I am trying to print the current date and time but it is printing blank on screen.
CurrentDate:[#th:block th:utext="${#temporals.format(now, 'dd/MMM/yyyy HH:mm')}"/]
I have added the Java8 date time dependency in pom.xml and also added the java8dialect in the template resolver bean.
pom.xml
--------
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
ThymeleafConfig.java
----------------------
@Configuration
public class ThymeleafConfig {
@Bean(name = "textTemplateEngine")
public TemplateEngine textTemplateEngine() {
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.addTemplateResolver(textTemplateResolver());
templateEngine.addDialect(new Java8TimeDialect());
return templateEngine;
}
private ITemplateResolver textTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("/templates/text/");
templateResolver.setSuffix(".txt");
templateResolver.setTemplateMode(TemplateMode.TEXT /* https://github.com/thymeleaf/thymeleaf/issues/395 */);
templateResolver.setCharacterEncoding("UTF8");
templateResolver.setCheckExistence(true);
templateResolver.setCacheable(false);
return templateResolver;
}
}
Can anybody please tell me what am I doing wrong in thymealeaf text template that it is not printing the date?
You can use a Thymeleaf utility method to print various date and time variants.
For example:
<div th:text="${#dates.createNow()}"></div>
I think that may answer your specific question.
However, I suspect your question is more about the wider topic of binding Java objects to Thymeleaf templates in Spring - and I have not used Spring with Thymeleaf.