视图应用
资源(模板来源 ) | 上下文(变量来源 ) | 模板引擎(模板渲染) |
通用资源抽象:File、ClassLoader、URL、ServletContext Spring 资源抽象:Resource | Thyemeaf:Context Spring Web MVC:Model Servlet:Attribute | Thymeleaf:TemplateEngine Spring:SpringTemplateEngine Spring WebFlux:SpringWebFluxTemplateEngine |
模板来源:Resource。变量来源:Context。模板渲染:SpringTemplateEngine
public class ThymeleafTemplateEngineBootstrap {
public static void main(String[] args) throws IOException {
// 构建引擎
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
// 创建渲染上下文
Context context = new Context();
context.setVariable("message", "Hello,World");
// 读取内容从 classpath:/templates/thymeleaf/hello-world.html
// ResourceLoader
ResourceLoader resourceLoader = new DefaultResourceLoader();
// 通过 classpath:/templates/thymeleaf/hello-world.html Resource
Resource resource = resourceLoader.getResource("classpath:/templates/thymeleaf/hello-world.html");
File templateFile = resource.getFile();
// 文件流
FileInputStream inputStream = new FileInputStream(templateFile);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// Copy
IOUtils.copy(inputStream, outputStream);
inputStream.close();
// 模板的内容
String content = outputStream.toString("UTF-8");
// 渲染(处理)结果
String result = templateEngine.process(content, context);
// 输出渲染(处理)结果
System.out.println(result);
}
}
Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。
1、六种模式格式
- HTML (网页)
- XML (配置)
- TEXT (文本)
- JAVASCRIPT (js)
- CSS (样式)
- RAW (无格式)
2、简单demo
- pom
<!-- Thymeleaf 自动配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 允许使用非严格的 HTML 语法 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
- application.yml
spring:
thymeleaf:
cache: false # 开发时关闭缓存,不然没法看到实时页面
mode: HTML # 用非严格的 HTML
encoding: UTF-8
servlet:
content-type: text/html
public class PersonBean implements Serializable {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@Controller
@RequestMapping(value = "thymeleaf")
public class IndexController {
@RequestMapping(value = "index", method = RequestMethod.GET)
public String index(Model model) {
PersonBean person = new PersonBean();
person.setName("张三");
person.setAge(22);
List<PersonBean> people = new ArrayList<>();
PersonBean p1 = new PersonBean();
p1.setName("李四");
p1.setAge(23);
people.add(p1);
PersonBean p2 = new PersonBean();
p2.setName("王五");
p2.setAge(24);
people.add(p2);
PersonBean p3 = new PersonBean();
p3.setName("赵六");
p3.setAge(25);
people.add(p3);
model.addAttribute("person", person);
model.addAttribute("people", people);
return "index";
}
}
resources/templates/index.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hello Thymeleaf</title>
</head>
<body>
<div>
<span>访问 Model:</span><span th:text="${person.name}"></span>
</div>
<div>
<span>访问列表</span>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr th:each="human : ${people}">
<td th:text="${human.name}"></td>
<td th:text="${human.age}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
使用语法