springboot使用thymeleaf生成静态页面
静态网页:模板+数据
示例:
在resources\templates下编写thymeleaf模板 hello.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${hello}"></h1>
</body>
</html>
@Autowired
private TemplateEngine templateEngine; //使用TemplateEngine对象
public void buildPage() throws IOException {
//数据
Context context = new Context();
context.setVariable("hello", "hello thymeleaf!");
//文件输出的路径及文件名
FileWriter writer = new FileWriter("E:\\temp\\page\\hello.html");
templateEngine.process("hello", context, writer); //参数:模板,数据,文件输出流
//关闭文件
writer.close();
}