一、maven引入jar包
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
二、编写读取模板工具类
import java.io.IOException;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FreemarkerUtil {
public Template getTemplate(String name) {
Configuration cfg = new Configuration();
cfg.setDefaultEncoding("UTF-8");
cfg.setClassForTemplateLoading(this.getClass(), "/com/szzt/tmp/common/mail/flt");
try {
Template template = cfg.getTemplate(name);
return template;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
三、模板文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<style type="text/css">
</head>
<body>
<div class="order-header">
<p>亲爱的用户,您好:</p>
</div>
<div class="order-table-box">
<div class="order-table-bg">
<div class="table-title">农行有新的订单,请您及时处理</div>
<!-- table -->
<div class="table-box">
<table class="order-table">
<thead>
<tr>
<th>合同号</th>
<th>状态描述</th>
<th>设备ID</th>
<th>设备型号</th>
<th>厂商名称</th>
<th>银行机构名称</th>
<th>采购批次号</th>
<th>收货地址</th>
<th>收货人电话</th>
<th>收货人姓名</th>
<th>发货开始时间</th>
<th>最晚发货时间</th>
</tr>
</thead>
<tbody>
<#list orderList as item>
<tr>
<td>${item.contractNo?default('')}</td>
<td>${item.description?default('')}</td>
<td>${item.deviceId?default('')}</td>
<td>${item.deviceModel?default('')}</td>
<td>${item.deviceVendorName?default('')}</td>
<td>${item.orgName?default('')}</td>
<td>${item.purchaseBatchNo?default('')}</td>
<td>${item.receiveAddress?default('')}</td>
<td>${item.receiverMobile?default('')}</td>
<td>${item.receiverName?default('')}</td>
<td>${item.sendBeginTime?default('')}</td>
<td>${item.sendLastTime?default('')}</td>
</tr>
</#list>
</tbody>
</table>
</div>
<!-- table -->
</div>
</div>
</body>
</html>
四、获取发送内容
Template template = new FreemarkerUtil().getTemplate("OrderNotice.flt");
String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);