Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。
Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
官方文档
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
spring:
thymeleaf:
cache: false
encoding: UTF-8
在resources/templates/目录中创建xxx.html。
这里创建了hello.html。
DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>titletitle>
head>
<body>
<div th:text="${key}">div>
body>
html>
@Controller
@RequestMapping("/test")
public class TestController {
/**
* 访问test/hello,返回hello.html
*/
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("key","hello world");
return "hello";
}
}
| 标签 | 作用 | |
|---|---|---|
| th:id | 替换id | |
| th:text | 文本替换 | |
| th:utext | 支持html的文本替换 | |
| th:object | 替换对象 | |
| th:value | 替换值 | |
| th:each | 迭代 | |
| th:href | 替换超链接 | |
| th:src | 替换资源 |
${Key}
<ul th:each="item:${MyList}">
<li th:text="${item}">li>
ul>
@{资源地址}
#{key}