Top Java Template Engines


This post gives you a quick overview of commonly used template engines in Web application development in Java/Java EE.

Java Template Engines

1. Apache Velocity

Velocity is a project of the Apache Software Foundation, charged with the creation and maintenance of open-source software related to the Apache Velocity Engine.

Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.

2. Apache 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.

3. Thymeleaf

It is a template engine capable of processing and generating HTML, XML, JavaScript, CSS and 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-thymeleafdependency:
<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. Apache Tiles

Apache Tiles is a templating framework built to simplify the development of web application user interfaces. 

Read more at the official website: http://tiles.apache.org.

5. Mustache.java

Implementation of mustache.js (web application templating system) for Java.

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

http://docs.groovy-lang.org/docs/next/html/documentation/template-engines.html

6. Groovy

This template engine is a template engine primarily aimed at generating XML-like markup (XML, XHTML, HTML5, … ), but that can be used to generate any text-based content.

Spring MVC views can also be generated using the Groovy Markup Template Engine. This 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.

Comments