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

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

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
spring.thymeleaf.cache=false
# Specify the template path
spring.thymeleaf.prefix=classpath:/templates/
# Set up thymeleaf Strictly check
spring.thymeleaf.mode=html
# Set up thymeleaf Template suffix
spring.thymeleaf.suffix=.html
# Set encoding
spring.thymeleaf.encoding=utf-8

 

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;
@Controller
public class CarController {
@RequestMapping("/getCar")
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">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
getCar
</body>
</html>

 

7、 Start project , And access

http://localhost:8888/getCar

 

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