Springboot integrates thymeleaf templates

Programmer light 2022-02-13 07:58:52 阅读数:929

springboot integrates thymeleaf templates

1、 Add template dependency

because springboot Many frameworks have been integrated , So we add something like thymeleaf Other framework , There is no need to add a version number , Write dependencies directly


2、 analysis SpringBoot Dependency relationship

In our project pom.xml In file , keyboard Ctrl Key press and hold , Click the left mouse button to enter the source code / The upper layer of the document

Here you can see :spring-boot-starter-parent Of pom.xml Medium parent modular

We continue Ctrl+ Left mouse button , Get in the upper floor pom

Here is the SpringBoot The top floor of :spring-boot-dependencies

If springboot Integrated a framework , Here you can search , If the search fails , Explain that there is no integration

Let's search here thymeleaf,Ctrl+F You can search for content

We can see SpringBoot2.5.6 Version integration Thymeleaf The version is 3.0.12


3、 To configure thymeleaf Templates

# Whether to enable cache is set to true
# Specify the template path
# Set up thymeleaf Strictly check
# Set up thymeleaf Template suffix
# Set encoding


4、 establish templates Folder

stay resources Create under directory templates Folder , by thymeleaf Template folder for


5、 Write controller Interface

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
public class CarController {
public String getCar(){
return "getCar";

6、 Write thymeleaf Templates

Create a template file in the template folder html that will do

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">


7、 Start project , And access



copyright:author[Programmer light],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130758496616.html