VOOZH about

URL: https://www.javacodegeeks.com/2016/05/thymeleaf-3-get-started-quickly-thymeleaf-3-spring-mvc.html

⇱ Thymeleaf 3 - Get Started Quickly with Thymeleaf 3 and Spring MVC - Java Code Geeks


Thymeleaf 3 release arrived. The new version brings plenty of new features like HTML5 support as well as Text templates support with no markup – [# th:utext="${thymeleaf.version}" /] , improved inline capabilities – <p>Thymeleaf [[${thymeleaf.version}]] is great!</p>, performence improvements and much more.

The easiest way the get starter with Thymeleaf 3 and Spring MVC is by using Spring MVC 4 Quickstart Maven Archetype. The archetype was updated to support Thymeleaf 3. The changes that are made to the archetype are described below.

Dependencies

The project uses Spring Platform BOM for dependencies management, but it does not yet (as time of writing this post) declare dependency on Thymeleaf 3, so I needed to declare the versions manually.

  • Thymeleaf:
<dependency>
 <groupId>org.thymeleaf</groupId>
 <artifactId>thymeleaf</artifactId>
 <version>3.0.0.RELEASE</version>
</dependency>
  • Thymeleaf Spring 4:
<dependency>
 <groupId>org.thymeleaf</groupId>
 <artifactId>thymeleaf-spring4</artifactId>
 <version>3.0.0.RELEASE</version>
</dependency>
  • Thymeleaf Spring Security 4:
<dependency>
 <groupId>org.thymeleaf.extras</groupId>
 <artifactId>thymeleaf-extras-springsecurity4</artifactId>
 <version>3.0.0.RELEASE</version>
</dependency>

The application generated with the archetype uses Java 8 Time Dialect and since Thymeleaf API changed, the dialect dependency must be updated too. Before it is available in Maven Central, we must add snapshot repository to POM:

<repository>
 <id>sonatype-nexus-snapshots</id>
 <name>Sonatype Nexus Snapshots</name>
 <url>https://oss.sonatype.org/content/repositories/snapshots</url>
 <snapshots>
 <enabled>true</enabled>
 </snapshots>
</repository>

And then declare the dependency:

<dependency>
 <groupId>org.thymeleaf.extras</groupId>
 <artifactId>thymeleaf-extras-java8time</artifactId>
 <version>3.0.0-SNAPSHOT</version>
</dependency>

Configuration changes

  • Template resolver

Template resolver before:

@Bean
public TemplateResolver templateResolver() {
 TemplateResolver resolver = new ServletContextTemplateResolver();
 resolver.setPrefix(VIEWS);
 resolver.setSuffix(".html");
 resolver.setTemplateMode("HTML5");
 resolver.setCacheable(false);
 return resolver;
}

Template resolver after:

@Bean
public ITemplateResolver templateResolver() {
 SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
 resolver.setPrefix(VIEWS);
 resolver.setSuffix(".html");
 resolver.setTemplateMode(TemplateMode.HTML);
 resolver.setCacheable(false);
 return resolver;
}
  • Template Engine
@Bean
public SpringTemplateEngine templateEngine() {
 SpringTemplateEngine templateEngine = new SpringTemplateEngine();
 templateEngine.setTemplateResolver(templateResolver());
 templateEngine.addDialect(new SpringSecurityDialect());
 templateEngine.addDialect(new Java8TimeDialect());
 return templateEngine;
}
  • View Resolver:
@Bean
public ViewResolver viewResolver() {
 ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
 thymeleafViewResolver.setTemplateEngine(templateEngine());
 thymeleafViewResolver.setCharacterEncoding("UTF-8");
 return thymeleafViewResolver;
}

Templates

The templates did not change in this project. But if you are migrating a real project, you may be interested in reading migration guide.

References

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Rafal Borowiec
Rafal Borowiec
May 15th, 2016Last Updated: May 14th, 2016
0 142 1 minute read

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz