场景
由于业务中台的邮件模板无法支持FreeMarker语法,故需自己编写html代码当做变量参数传入邮件模板,所以最终选用了引入FreeMarker模板引擎实现。
添加POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.4.5</version>
</dependency>
添加模板
<table style='width:100.0%;border:1.0px solid #e5e5e5;margin-bottom:15.0px;border-collapse:collapse;font-size:15px'>
<th style='width:40px;border:1.0px solid #e5e5e5;background-color:#f5f5f5;padding:5.0px;text-align:center;'>序号</th>
<th style='width:150px;border:1.0px solid #e5e5e5;background-color:#f5f5f5;padding:5.0px;text-align:center;'>姓名</th>
<#list dataList as item>
<tr>
<td style='padding:5.0px;text-align:center;border:1.0px solid #e5e5e5;'>${item.number}</td>
<td style='padding:5.0px;text-align:center;border:1.0px solid #e5e5e5;'>${item.name}</td>
</tr>
</#list>
</table>
添加工具类
package com.example.cabin;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.StringWriter;
public class FreeMarkerUtil {
private FreeMarkerUtil() {
}
public static String parseTemplate(String path, String filename, Object data) throws IOException {
Configuration configuration = new Configuration(Configuration.getVersion());
configuration.setDefaultEncoding("UTF-8");
configuration.setClassForTemplateLoading(FreeMarkerUtil.class, path);
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
configuration.setWhitespaceStripping(true);
Template template = configuration.getTemplate(filename);
// 接收处理后的模版内容
StringWriter stringWriter = new StringWriter();
try {
template.process(data, stringWriter);
return stringWriter.toString();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
stringWriter.close();
}
return "";
}
}
编写测试方法
package com.example.cabin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
private static final String path = "/";
private static final String filename = "demo.ftl";
public static void main(String[] args) throws IOException {
//模拟对象
Map<String, String> object = new HashMap<>(2);
object.put("number", "1");
object.put("name", "xxx");
//数据列表
List<Map<String, String>> dataList = new ArrayList<>();
dataList.add(object);
//模板变量
Map<String, List<Map<String, String>>> templateMap = new HashMap<>(1);
templateMap.put("dataList", dataList);
//调用工具类抓取html代码
String template = FreeMarkerUtil.parseTemplate(path, filename, templateMap);
System.out.println(template);
}
}
输出结果
<table style='width:100.0%;border:1.0px solid #e5e5e5;margin-bottom:15.0px;border-collapse:collapse;font-size:15px'>
<th style='width:40px;border:1.0px solid #e5e5e5;background-color:#f5f5f5;padding:5.0px;text-align:center;'>序号</th>
<th style='width:150px;border:1.0px solid #e5e5e5;background-color:#f5f5f5;padding:5.0px;text-align:center;'>姓名</th>
<tr>
<td style='padding:5.0px;text-align:center;border:1.0px solid #e5e5e5;'>1</td>
<td style='padding:5.0px;text-align:center;border:1.0px solid #e5e5e5;'>xxx</td>
</tr>
</table>
对应效果:
序号 | 姓名 |
---|---|
1 | xxx |
最后看下整体项目结构
无需配置application.yml
声明:
如涉及侵权请联系作者及时删除