SpringBoot2.1.5 (33) --- SpringBoot integration Thymeleaf template engine

 

table of Contents

 

A, Thymeleaf

Summary:

Thymeleaf modern server-side Java template engine for the Web and independent environment.

The main objective is to Thymeleaf template to the natural elegance of your development workflow -HTML display correctly in the browser, and can be used as a static prototype, in order to achieve a more powerful collaboration across the development team. Thymeleaf can handle HTML, XML, JavaScript, CSS or even plain text.

Thymeleaf main goal is to provide an elegant and highly maintainable way to create a template. To achieve this, it is built on the concept of natural template, the template will not affect its logic is injected into the template file as a way to design the prototype. This improves the design communication and understanding to bridge the difference between the front-end design and development staff.

Thymeleaf is designed from the outset (particularly HTML5) allows you to create a template fully validated.

Official website

https://www.thymeleaf.org/

Official Documents

https://www.thymeleaf.org/documentation.html

Introductory information Reference:

https://blog.csdn.net/zrk1000/article/details/72667478

Second, the code practice

1, maven dependence

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2, yml configuration file

server:
  port: 8081
  
spring:
  application:
    name: thymeleaf

  thymeleaf:
    # 是否启用模板缓存。
    cache: true
    # 是否检查模板位置是否存在。
    check-template: true
    # 是否为Web框架启用Thymeleaf视图分辨率。
    enabled: true
    # 编码格式, 默认UTF-8
    encoding: UTF-8
    # 应用于模板的模板模式。另请参阅Thymeleaf的TemplateMode枚举。
    mode: HTML
    # 后缀 默认 .html
    suffix: .html
    # 模板文件存放位置  , 默认 classpath:/templates/
    prefix: classpath:/templates/

3, Thymeleaf file

Create a template file index.html, document reads as follows

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1 th:text="${content}"></h1>
</body>
</html>

4. Test Controller

/**
 * created with IntelliJ IDEA.
 * author: fxbin
 * date: 2018/10/21
 * time: 4:42
 * version: 1.0
 * description:
 */
@Controller
public class ThymeleafController {

    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("content", "Hello Thymeleaf");
        return "index";
    }
}

5, start the project, testing

Access  HTTP: // localhost: 8081 / the Test  , we can see that the content provided in the controller is successfully displayed on the page
Here Insert Picture Description

 

 

Guess you like

Origin blog.csdn.net/zhangbijun1230/article/details/91357519