Spring Boot Series (V) web Development - Thymeleaf, FreeMarker Template Engine

The previous articles described how returning json data provides a good RESTful api, and now we show how to render the processed data onto the page.

Spring Boot uses a template engine

Spring Boot recommends template engines such as Thymeleaf, FreeMarker, Velocity, Groovy, Mustache, etc. JSP is not recommended.

Spring Boot provides a good default configuration for the above engines. The default src/main/resources/templates directory is the configuration path for the above template engines.

I. Using Thymeleaf Template Engine in Spring Boot

Introduction: Thymeleaf is a template engine similar to Velocity and FreeMarker, which can be used for application development in both Web and non-Web environments, and can completely replace JSP.

1. Adding dependencies to pom.xml

<!-- thymeleaf template engine-->

2. Writing controller

 * @author sam
 * @since 2017/7/16
public class HomeController {

    public String home(ModelMap modelMap) {

        modelMap.put("name", "Magical Sam");

        List<String> list = new ArrayList<>();
        list.add("sam a");
        list.add("sam b");
        list.add("sam c");
        list.add("sam d");
        modelMap.put("list", list);

        return "home";


3. Write html code, where th:text="${name}" is the syntax of thymeleaf, which can be referred to specifically: Thymeleaf official document

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8"/>
    <span th:text="${name}"></span>
        <li th:each="item : ${list}" th:text="${item}"></li>

Start the application, access: http://localhost:8080/home The corresponding results can be obtained.

If you need to modify the default configuration of thymeleaf, you can add:

# ================================================
#                   Thymeleaf configuration
# ================================================
# Whether thymeleaf template parsing is enabled
# Whether to turn on template caching (Recommendation: Set the development environment to false and the production environment to true)
# Check that the templates location exists.
# Template media type settings, default text/html
# Encoding settings for templates, default UTF-8
# Set up views that can be parsed, separated by commas
# Exclude views that do not need to be parsed, separated by commas
# Template mode settings, default HTML5
# Prefix settings, SpringBook default template placed in classpath:/template/directory
# Suffix settings, default to. html
# The order in which templates are parsed in the template chain

2. Using FreeMarker Template Engine in Spring Boot

1. Adding dependencies to pom.xml

<!-- freemarker template engine -->

2. Writing controller


3. Create a new home.ftl file under templates to write html code. freemarker grammar can be referred to: FreeMarker official document

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <#list list as item >

Start the application, access: http://localhost:8080/home The corresponding results can be obtained.

If you need to modify the default configuration of freemarker, you can add:

# ================================================
#                   FreeMarker configuration
# ================================================
# Whether to turn on template caching
# Encoding format
# Media Type Settings for Templates
# The prefix is set by default to ""
# The suffix is set to.ftl by default.

Appendix: application.properties all configuration items, Click to view all configuration instructions for Spring Boot

Copyright Statement: This article is the original article of the blogger. Please indicate the source for reprinting.

Tags: Java Spring Thymeleaf FreeMarker encoding

Posted on Mon, 11 Feb 2019 03:36:17 -0800 by pneudralics