Thymeleaf实现页面静态化

目录
  1. 添加依赖
  2. 配置测试模板 test.html
  3. 配置Bean
  4. 配置相关工具类
  5. 业务编写

最近业余时间在折腾CMS框架,需要完成页面静态化。采用的方案是Thymeleaf。
大致流程是:首先需要编辑一些Thymeleaf模板,然后调用接口来向模板里渲染数据,同时输出到指定文件。

添加依赖

项目使用的是Spring Boot,所以直接使用spring-boot-starter-thymeleaf。

1
2
3
4
5
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>

配置测试模板 test.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello <span th:text="${title}">test</span></title>
</head>
<script th:remove="all" type="text/javascript" src="http://cdn.jsdelivr.net/thymol/latest/thymol.min.js"></script>
<body>
<section class="section">
<div class="container">
<h1 class="title">
Hello World
</h1>
<p class="subtitle">
My first website with <span th:text="${content}">test</span>!
</p>
</div>
</section>
</body>
</html>

将test.html放入resources/templates下即可。

配置Bean

生成Bean供业务使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Configuration
public class AppConfig {

@Bean
public TemplateEngine emailTemplateEngine() {
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(springTemplateResolver());
return templateEngine;
}

private ITemplateResolver springTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setOrder(1);
templateResolver.setPrefix("classpath:/templates");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
}

在对应的业务类中注入即可。

配置相关工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class ThymeleafUtil {

// 隐藏Thymeleaf细节, 直到在渲染数据前都使用Map存储数据
public static Context getContext(Map<String, Object> dataMap) {
Context ctx = new Context();
for (Map.Entry<String, Object> dataEntry : dataMap.entrySet()) {
ctx.setVariable(dataEntry.getKey(), dataEntry.getValue());
}
return ctx;
}

}


import org.apache.commons.io.FileUtils;
public class FileUtil {

// 输出内容到文件
public static void writeStringToFile(String content, String filePath) {
File file = new File(filePath);
try {
FileUtils.writeStringToFile(file, content, Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace(); // TODO
}
}

}

业务编写

注入TemplateEngine

1
2
@Autowired
private TemplateEngine templateEngine;
1
2
3
4
5
Map<String, Object> dataMap = Maps.newHashMap();
dataMap.put("title", "test");
dataMap.put("content", "test");
Context ctx = ThymeleafUtil.getContext(dataMap);
FileUtil.writeStringToFile(templateEngine.process("test", ctx), "result.html");

完成。

Thymeleaf相关链接:

Thymeleaf List指定数量循环
Thymeleaf SAXParseException
Thymeleaf TemplateProcessingException
Thymeleaf比较判断枚举类型