Spring Boot with Template Engines


This post gives you an overview of template engines supported by spring boot.
Spring MVC supports a variety of templating technologies, including Thymeleaf, FreeMarker, and JSPs. Also, many other templating engines include their own Spring MVC integrations.

Spring Boot includes auto-configuration support for the following templating engines:
  1. FreeMarker
  2. Groovy
  3. Thymeleaf
  4. Mustache
Let's look at the overview and usage of each above template engine with spring boot.

1. FreeMarker 

Apache FreeMarker is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data. 

In a Spring Boot application, we can simplify the needed configuration by using the spring-boot-starter-freemarker dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

This starter adds the necessary auto-configuration. All we need to do is start placing our template files in the resources/templates folder.

2. Groovy

This template engine is based on a builder syntax and can be used for generating any text format.
Spring Boot contains auto-configuration for the Groovy Template Engine, which is added by including the spring-boot-starter-groovy-templates dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-groovy-templates</artifactId>
</dependency>

The default location for the templates is /resources/templates.

3. Thymeleaf

 It is a template engine capable of processing and generating HTML, XML, JavaScript, CSS, text, and can work both in web and non-web environments. It is better suited for serving the view layer of web applications, but it can process files in many formats, even in offline environments.

Spring Boot will provide auto-configuration for Thymeleaf by adding the spring-boot-starter-thymeleaf dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Thymeleaf is widely used with Spring boot/Spring MVC to develop Spring-based web applications.

4. Mustache

JMustache is a template engine that can be easily integrated into a Spring Boot application by using the spring-boot-starter-mustache dependency.

Spring Boot will provide auto-configuration for Mustache by adding the spring-boot-starter-mustache dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mustache</artifactId>
</dependency>
When you use one of these templating engines with the default configuration, your templates are picked up automatically from src/main/resources/templates.

Spring boot team recommended: If possible, JSPs should be avoided. There are several known limitations when using them with embedded servlet containers.

To know how to use Spring with these template engines at Template Engines for Spring.

Check out more at official documentation at https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

Comments