1.引入maven 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2. 配置文件设置
############################################## # # thymeleaf静态资源配置 # ############################################## # 默认路径 spring.thymeleaf.prefix=classpath:/templates/ # 后缀 spring.thymeleaf.suffix=.html # 模板格式 spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false
3. controller 类
@RequestMapping("index/") public String index(ModelMap map){ map.addAttribute("name","haozz"); return "index"; }
4.index.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Index thymeleaf</title> <link rel="stylesheet" type="text/css" href="css/main.css" th:href="@{/css/main.css}"/> <link rel="stylesheet" type="text/css" href="js/main.js" th:href="@{/js/main.js}"/> </head> <body> <h1>HELLO INDEX THYMELEAF</h1> 你好,<h2 th:text="${name}"></h2> <p class="login-title">哈哈 NBA</p> <img src="/images/1.jpg" th:href="@{/images/1.jpg}"/> </body> </html>
5.资源文件