1、引入pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、设置thymeleaf版本,版本3检查html标签可以没有闭合结束符
<properties> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version> </properties>
3、配置文件设置thymeleaf属性
spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML5
4、在resources/templates新建index.html文件
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello World!</title> </head> <body> <h1 th:inline="text">Hello</h1> <p th:text="${hello}"></p> </body> </html>
5、编写controller
@RequestMapping @Controller public class IndexController { @GetMapping("/helloHtml") public String testHelloHtml(Model model){ model.addAttribute("hello", "你好"); return "index"; } }