This post gives you an overview of template engines supported by spring boot.
Spring Boot includes auto-configuration support for the following templating engines:
Let's look at overview and usage of each above template engine with 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.
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> <version>2.1.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.
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> <version>2.1.2.RELEASE</version> </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> <version>2.1.2.RELEASE</version> </dependency>
Thymeleaf is widely used with Spring boot/Spring MVC to develop Spring-based web applications.
Refer below articles to know more about how to use Thymeleaf with spring MVC/spring boot:
4. Mustache
JMustache is a template engine which 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:
Spring boot team recommended: If possible, JSPs should be avoided. There are several known limitations when using them with embedded servlet containers.
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> <version>2.1.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.
To know how to use Spring with these template engine at Template Engines for Spring.
Check out more at official documentation at https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
Related Spring Boot Posts
>> Installing Spring Boot with Maven and Gradle
>> Standard Project Structure for Spring Boot Projects // LATEST
>> Standard Project Structure for Spring Boot Projects // LATEST
>> Important Spring Boot Starters with Examples
>> Spring Boot 2 Deploy WAR file to External Tomcat
>> SpringBootServletInitializer Class in Spring Boot // LATEST
>> Different Ways of Running Spring Boot Application
>> Migrating from Spring to Spring Boot
>> Spring Boot Annotations
>> SpringApplication Class in Spring Boot with Examples // LATEST
>> SpringBootServletInitializer Class in Spring Boot // LATEST
>> Different Ways of Running Spring Boot Application
>> Migrating from Spring to Spring Boot
>> Spring Boot Annotations
>> SpringApplication Class in Spring Boot with Examples // LATEST
>> Spring Boot 2 Logging SLF4j Logback and LOG4j2 Example
>> Spring Boot 2 + Jersey REST + JPA + Hibernate 5 CRUD REST APIs Example
>> Spring Boot 2 - Scheduling Tasks
>> Spring Boot 2 RESTful API Documentation with Swagger 2 Tutorial
>> Spring Boot 2 - File Upload and Download Rest API Tutorial // LATEST
>> Spring Boot - Loading Initial Data
>> Spring Boot 2 + Jersey REST + JPA + Hibernate 5 CRUD REST APIs Example
>> Spring Boot 2 - Scheduling Tasks
>> Spring Boot 2 RESTful API Documentation with Swagger 2 Tutorial
>> Spring Boot 2 - File Upload and Download Rest API Tutorial // LATEST
>> Spring Boot - Loading Initial Data
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment