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:
Let’s look at the overview and usage of each above template engine with spring boot.
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
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <version>2.2.2.RELEASE</version> </dependency>
This starter adds the necessary auto-configuration. All we need to do is start placing our template files in the resources/templates folder.
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:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-groovy-templates --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-groovy-templates</artifactId> <version>2.2.2.RELEASE</version> </dependency>
The default location for the templates is /resources/templates.
Spring Boot will provide auto-configuration for Thymeleaf by adding the spring-boot-starter-thymeleaf dependency:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.2.2.RELEASE</version> </dependency>
Thymeleaf is widely used with Spring boot/Spring MVC to develop Spring-based web applications.
Related Spring Boot Posts
- Deploy Spring Boot WAR to Tomcat Server
- Spring boot custom JSON Serialize – Deserialize Example
- Custom Banners in Spring Boot
- Managing Spring Boot Exit Code
- Introduction to the SpringBootServletInitializer
- Building web applications with Spring Boot and Kotlin
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:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mustache --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mustache</artifactId> <version>2.2.2.RELEASE</version> </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.