“This is the first day of my participation in the Gwen Challenge in November. See details of the event: The last Gwen Challenge in 2021”.

1. Introduction

Spring Boot provides a number of template engines, including: FreeMarker /Groovy /Mustache /Thymeleaf /Velocity/ Beetl.

  • Why Thymeleaf?

Spring Boot officially recommends Thymeleaf as the template engine for the following three main reasons:

  1. Thymeleaf works with and without a network. It supports HTML prototypes, and then adds additional attributes to HTML tags to achieve the template + data presentation. Many companies that do not have clear business separation between the front and back end reduce the front end personnel burden.

  2. Thymeleaf features out of the box. It provides both standard and Spring standard dialects, which can be used directly from templates or extended and created with custom dialects.

  3. Thymeleaf provides perfect Spring MVC support for fast form binding, property editor, internationalization, and more.

  • How to use it?

Now Thymeleaf has been updated to version 3.0, you can go to the website: www.thymeleaf.org/ to see how to use the tutorial to learn, let me explain how to integrate SpringBoot with Thymeleaf!

Integration of 2.

  • When SpringBoot is created, select Thymeleaf in the engine

  • Add the dependent

The spring-boot-starter-thymelea dependency is added to pum. XML for automatic configuration. Since Thymeleaf allows us to add additional attributes to HTML tags, we may be using non-strict HTML syntax, so we need to add a nekoHTML dependency to solve this problem.

        <! Neko supports non-strict syntax -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        
         <! -- thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

Copy the code
  • Configure Thymeleaf in the YML file

There are two main configurations:

Cache: false # It is recommended to turn caching off during development

The default is HTML5

Note: Using spring.thymeleaf. Mode =LEGCYHTML5 to remove validation is to avoid strict validation of tags, which is why the NekoHTML package was introduced.

spring:
  thymeleaf:
    encoding: UTF-8  # Encoding specification default
    cache: false # It is recommended to turn caching off during development
    prefix: classpath:/templates/ 
    suffix: .html
    mode: LEGACYHTML5 The default is HTML5
    servlet:
      content-type: text/html
Copy the code
  • Creating HTML pages

Create the index.html file in the Templates directory,

Modify the HTML tag to introduce the Thymeleaf engine so that the th:* syntax can be used in other tags as follows:

<! DOCTYPEhtml SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html  xmlns:th="http://www.thymeleaf.org">
Copy the code

Code:

<! DOCTYPEhtml>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>The home page</title>
</head>
<body>The HTML page of the template engine home page<p th:text="${msg}">Information display</p>
</body>
</html>
Copy the code

This completes the springboot integration of Thymeleaf, and the next step is to write a control layer to request access or request access directly, which is not described here!

  • Additional knowledge

SpringBoot using thymeleaf as view to define the template files in the SRC/main/resource/templates directory, namely the templates folder, is placed a template file, so you need to view the parser to parse it. If you want to access the HTML directly, place it in the static directory.