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-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2. Writing controller

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

    @RequestMapping("/home")
    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">
<head>
    <meta charset="UTF-8"/>
    <title>Home</title>
</head>
<body>
    <span th:text="${name}"></span>
    <ul>
        <li th:each="item : ${list}" th:text="${item}"></li>
    </ul>
</body>
</html>

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
spring.thymeleaf.enabled=true
# Whether to turn on template caching (Recommendation: Set the development environment to false and the production environment to true)
spring.thymeleaf.cache=false 
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true 
# Template media type settings, default text/html
spring.thymeleaf.content-type=text/html
# Encoding settings for templates, default UTF-8
spring.thymeleaf.encoding=UTF-8
# Set up views that can be parsed, separated by commas
#spring.thymeleaf.view-names=
# Exclude views that do not need to be parsed, separated by commas
#spring.thymeleaf.excluded-view-names=
# Template mode settings, default HTML5
#spring.thymeleaf.mode=HTML5 
# Prefix settings, SpringBook default template placed in classpath:/template/directory
spring.thymeleaf.prefix=classpath:/templates/ 
# Suffix settings, default to. html
spring.thymeleaf.suffix=.html
# The order in which templates are parsed in the template chain
#spring.thymeleaf.template-resolver-order=

2. Using FreeMarker Template Engine in Spring Boot

1. Adding dependencies to pom.xml

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

2. Writing controller

Ditto.

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

home.ftl
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <span>${name}</span>
    <ul>
    <#list list as item >
        <li>${item}</li>
    </#list>
    </ul>
</body>
</html>

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
spring.freemarker.cache=true
# Encoding format
spring.freemarker.charset=UTF-8
# Media Type Settings for Templates
spring.freemarker.content-type=text/html
# The prefix is set by default to ""
spring.freemarker.prefix=
# The suffix is set to.ftl by default.
spring.freemarker.suffix=.ftl
#spring.freemarker.allow-request-override=false
#spring.freemarker.check-template-location=true
#spring.freemarker.expose-request-attributes=false
#spring.freemarker.expose-session-attributes=false
#spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.request-context-attribute=
#spring.freemarker.template-loader-path=classpath:/templates/
#spring.freemarker.view-names=

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