读音
发音就是Time leaf
有些培训的会念成塞姆来福
其实念啥没问题
定义
Thymeleaf 是一个模板引擎,由于SpringBoot默认使用Thymeleaf,现逐步取代jsp。
特点
动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,无网络显示静态内容,有网络用后台得到数据替换静态内容。支持 html 原型,浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
与SpringBoot完美整合,springboot默认整合thymeleaf。提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
链接
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
使用方式
绑定
把html等文件放在resources的templates下。
在controller文件中写导向
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<h1>test?</h1>
</body>
</html>
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
//templates下的页面需要通过controller进行跳转,使用thymeleaf
public class IndexController {
@GetMapping("test")
public String test(){
return "test";
}
}
传值
@Controller
//templates下的页面需要通过controller进行跳转,使用thymeleaf
public class IndexController {
@GetMapping("test")
public void test(Model model){
model.addAttribute("msg","hello,Thymeleaf");
}
}
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<h1>test?</h1>
<div th:text="${msg}"></div>
</body>
</html>