This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

One. What is Thymeleaf

www.thymeleaf.org/ Thymeleaf is a modern server-side Java template engine for Web and standalone environments, capable of handling HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide an elegant and highly maintainable way to create templates. To do this, it builds on the concept of natural templates, injecting their logic into template files in a way that does not affect their use as design prototypes. This improves design communication and Narrows the gap between the design and development teams. Thymeleaf is an HTML5 template engine that can be used for application development in Web environments. Thymeleaf provides an optional module for integrating Spring MVC. You can use Thymeleaf to completely replace JSP or other templating engines such as Velocity, FreeMarker, etc. In application development. The main goal of Thymeleaf is to provide a well-formed way to create templates that will be displayed correctly by the browser. Thymeleaf template engine, which replaces JSP.

The Thymeleaf template is used in SpringBoot

1. Add the Thymeleaf dependency to pom. XML

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
Copy the code

2. Disable the Thymeleaf cache

Add the following code under Spring: in application.yml (to make changes to the page take effect in a timely manner, like hot deployment) :

# Can make changes to the page timely effect, similar to hot deployment effect
thymeleaf:
    cache: false
Copy the code

Note the indentation, after adding the indentation as follows:

3. Create the Thymeleaf template

Create a normal HTML file called hello.html as follows:

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>
Copy the code

Adding the namespace XMLNS :th=”http://www.thymeleaf.org” to the HTML tag indicates that the page is a Thymeleaf template page. Replace < HTML lang=”en”> with < HTML lang=”en” XMLNS :th=”http://www.thymeleaf.org”> so that you can use the TH attribute inside the tag in the page to fetch the value in the model, similar to an EL expression. The specific usage code is as follows:

<! DOCTYPEhtml>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p th:text="Welcome to China, my name is '+${name}+', this year '+${age}+'. '"></p>
    <p>Welcome to China, my name is<span th:text="${name}"></span>This year,<span th:text="${age}"></span>Years old.</p>
</body>
</html>
Copy the code

4. Create a class (to interact with the HTML page above)

ackage com.ysw.springboot01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/thy")
public class ThymeleafController {

    @RequestMapping("/hello")
    public String hello0(Model model){
        // Store data to the model
        model.addAttribute("name"."Li bai");
        model.addAttribute("age"."18");
        // Jump to the hello.html template engine
        return "hello"; }}Copy the code

5. Access the service path. The result is as follows: