一岂丘、引言
1.1 freemarker簡介
FreeMarker是一個用Java語言編寫的模板引擎储矩,它基于模板來生成文本輸出植影。FreeMarker與Web容器無關(guān),即在Web運行時颜价,它并不知道Servlet或HTTP。它不僅可以用作表現(xiàn)層的實現(xiàn)技術(shù)饵较,而且還可以用于生成XML拍嵌,JSP或Java 等。目前企業(yè)中循诉,主要用Freemarker做靜態(tài)頁面或是頁面展示横辆。
模板編寫為FreeMarker Template Language (FTL)。它是簡單的茄猫,專用的語言狈蚤, 不是 像PHP那樣成熟的編程語言。 那就意味著要準備數(shù)據(jù)在真實編程語言中來顯示划纽,比如數(shù)據(jù)庫查詢和業(yè)務(wù)運算脆侮, 之后模板顯示已經(jīng)準備好的數(shù)據(jù)。在模板中勇劣,你可以專注于如何展現(xiàn)數(shù)據(jù)靖避, 而在模板之外可以專注于要展示什么數(shù)據(jù)。
這種方式通常被稱為 MVC (模型 視圖 控制器) 模式比默,對于動態(tài)網(wǎng)頁來說幻捏,是一種特別流行的模式。 它幫助從開發(fā)人員(Java 程序員)中分離出網(wǎng)頁設(shè)計師(HTML設(shè)計師)命咐。設(shè)計師無需面對模板中的復(fù)雜邏輯篡九, 在沒有程序員來修改或重新編譯代碼時,也可以修改頁面的樣式醋奠。
而FreeMarker最初的設(shè)計榛臼,是被用來在MVC模式的Web開發(fā)框架中生成HTML頁面的伊佃,它沒有被綁定到 Servlet或HTML或任意Web相關(guān)的東西上。它也可以用于非Web應(yīng)用環(huán)境中沛善。
1.2 Freemarker的使用方法
把freemarker的jar包添加到工程中航揉。
1.2.1 Maven工程
添加依賴:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
1.2.2 非 Maven工程
加入相應(yīng)jar包到build path中
1.3 Freemarker的實現(xiàn)原理
1.4 使用步驟
第一步:創(chuàng)建一個Configuration對象,直接new一個對象金刁。構(gòu)造方法的參數(shù)就是freemarker對于的版本號迷捧。
第二步:設(shè)置模板文件所在的路徑。
第三步:設(shè)置模板文件使用的字符集胀葱。一般就是utf-8.
第四步:加載一個模板漠秋,創(chuàng)建一個模板對象。
第五步:創(chuàng)建一個模板使用的數(shù)據(jù)集抵屿,可以是pojo也可以是map庆锦。一般是Map。
第六步:創(chuàng)建一個Writer對象轧葛,一般創(chuàng)建一FileWriter對象搂抒,指定生成的文件名。
第七步:調(diào)用模板對象的process方法輸出文件尿扯。
第八步:關(guān)閉流求晶。
public void test() throws IOException, TemplateException {
// 第一步:創(chuàng)建一個Configuration對象,直接new一個對象衷笋。構(gòu)造方法的參數(shù)就是freemarker對于的版本號芳杏。
Configuration configuration = new Configuration(Configuration.getVersion());
// 第二步:設(shè)置模板文件所在的路徑。
configuration.setDirectoryForTemplateLoading(new File("D:\\Java\\Eclipse\\workspace_Test\\FreeMarker\\src\\main\\webapp\\WEB-INF\\ftl"));
// 第三步:設(shè)置模板文件使用的字符集辟宗。一般就是utf-8.
configuration.setDefaultEncoding("utf-8");
// 第四步:加載一個模板爵赵,創(chuàng)建一個模板對象。 Template template = configuration.getTemplate("hello.ftl");
// 第五步:創(chuàng)建一個模板使用的數(shù)據(jù)集泊脐,可以是pojo也可以是map空幻。一般是Map。
Map dataModel = new HashMap();
// 向數(shù)據(jù)集中添加數(shù)據(jù)
dataModel.put("hello", "this is my first freemarker test.");
// 第六步:創(chuàng)建一個Writer對象容客,一般創(chuàng)建一FileWriter對象秕铛,指定生成的文件名。
Writer out = new FileWriter(new File("D:\\Java\\Eclipse\\workspace_Test\\FreeMarker\\out\\hello.html"));
// 第七步:調(diào)用模板對象的process方法輸出文件缩挑。
template.process(dataModel, out);
// 第八步:關(guān)閉流但两。
out.close();
}
1.5 模板的基本語法
1.5.1 訪問map中的key
${key}
1.5.2 訪問pojo中的屬性
Student對象。學(xué)號调煎、姓名镜遣、年齡
${key.property}
<body>
取學(xué)生的信息:</br>
<label>學(xué)號:</label>${stu.id}<br>
<label>姓名:</label>${stu.name}<br>
<label>年齡 :</label>${stu.age}<br>
</body>
1.5.3 取集合中的數(shù)據(jù)
循環(huán)使用格式:
<#list 要循環(huán)的數(shù)據(jù) as 循環(huán)后的數(shù)據(jù)>
</#list>
例如:
<body>
<table boder=1>
<#list studentlist as student>
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
</tr>
</list>
</table>
</body>
List<Student> studentList=new ArrayList<>();
studentList.add(new Student(1,"張三",20));
studentList.add(new Student(2,"張三2",21));
studentList.add(new Student(3,"張三3",22));
studentList.add(new Student(4,"張三4",23));
studentList.add(new Student(5,"張三4",24));
modelMap.put("studentList",studentList);
Writer out=new FileWrite(new File("D://temp/first.html"));
1.5.4 取循環(huán)中的下標
<#list studentList as student>
${student_index}
</#list>
例如:
<body>
<table boder=1>
<#list studentlist as student>
<tr>
<td>${student_index}</td>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
</tr>
</list>
</table>
</body>
1.5.5 判斷
<#if student_index % 2 == 0>
<#else>
</#if>
例如:
<body>
<table boder=1>
<#list studentlist as student>
<#if student_index %2 ==0>
<tr bgcolor="blue">
<#else>
<tr bgcolor="red">
</list>
</table>
</body>
可以利用索引加上判斷己肮,實現(xiàn)表格中奇數(shù)和偶數(shù)行以不同的顏色來進行標識士袄。
1.5.6 日期類型格式化
直接取值:${date}(date是屬性名)如果傳來的是一個Date型數(shù)據(jù)會報錯
${date?date} //2016-9-13
${date?time} //17:53:55
${date?datetime} //2016-9-13 17:53:55
如果感覺freemaker提供的功能太弱悲关,可以在java中格式化好之后再通過普通字符串的形式傳入進來。
1.5.7 Null值的處理
如果直接取一個不存在的值(值為null)時會報異常
${aaa}
如果允許變量可以為空娄柳,并且不希望報出異常寓辱,可以在變量后面加上一個!
。
${aaa!}
1.5.8 Include標簽
如果希望在某個FTL文件中引入另外一個FTL文件赤拒,可以使用FTL關(guān)鍵字秫筏。格式如下:
<#include “模板名稱”> //(相當于jstl中的包含)
例如:
<#include "hello.ftl">
注意:上述引用方式表明hello.ftl
與當前文件處于同一個目錄下,如果兩個文件在不同的目錄下挎挖,需要使用相對路徑这敬。
二、需求描述
需要實現(xiàn)一個定時發(fā)送監(jiān)控數(shù)據(jù)報表的功能蕉朵,技術(shù)實現(xiàn)上考慮使用freemaker來實現(xiàn)郵件內(nèi)容的渲染崔涂。
三、實戰(zhàn)
3.1 引入包依賴
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>
3.2 編寫郵件模板
編寫的郵件模板report.ftl文件放在resources/report/report.ftl
路徑下始衅。模板的完整內(nèi)容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style type="text/css">
/* What it does: Remove spaces around the email design added by some email clients. */
/* Beware: It can remove the padding / margin and add a background color to the compose a reply window. */
html,
body {
margin: 0;
padding: 0;
height: 100% !important;
width: 100% !important;
}
/* What it does: Stops email clients resizing small text. */
* {
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}
/* What it does: Forces Outlook.com to display emails full width. */
.ExternalClass {
width: 100%;
}
/* What it does: Stops Outlook from adding extra spacing to tables. */
table,
td {
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
}
/* What it does: Fixes webkit padding issue. */
table {
border-spacing: 0 !important;
}
/* What it does: Fixes Outlook.com line height. */
.ExternalClass,
.ExternalClass * {
line-height: 100%;
}
/* What it does: Fix for Yahoo mail table alignment bug. Applies table-layout to the first 2 tables then removes for anything nested deeper. */
table {
border-collapse: collapse;
margin: 0 auto;
}
/* What it does: Uses a better rendering method when resizing images in IE. */
img {
-ms-interpolation-mode: bicubic;
}
/* What it does: Overrides styles added when Yahoo's auto-senses a link. */
.yshortcuts a {
border-bottom: none !important;
}
/* What it does: Overrides blue, underlined links auto-detected by iOS Mail. */
/* More Info: https://litmus.com/blog/update-banning-blue-links-on-ios-devices */
.mobile-link--footer a {
color: #666666 !important;
}
/* What it does: Overrides styles added images. */
img {
border: 0 !important;
outline: none !important;
text-decoration: none !important;
}
/* What it does: Apple Mail doesn't support max-width, so a media query constrains the email container width. */
@media only screen and (min-width: 801px) {
.email-container {
width: 960px !important;
}
}
/* What it does: Apple Mail doesn't support max-width, so a media query constrains the email container width. */
@media only screen and (max-width: 800px) {
.email-container {
width: 100% !important;
max-width: none !important;
}
}
table tr td .head {
padding: 10px 0;
font-family: "Microsoft YaHei", sans-serif;
text-align: center;
font-size: 12px;
line-height: 1.3;
color: #333333;
font-weight: bold;
word-wrap: break-word;
border-bottom: 1px solid #cccccc;
}
table tr td .text {
padding: 10px 0;
font-family: "Microsoft YaHei", sans-serif;
text-align: center;
font-size: 12px;
line-height: 1.3;
color: #333333;
border-bottom: 1px solid #eeeeee;
}
table tr td .htext {
padding: 10px 0;
font-family: "Microsoft YaHei", sans-serif;
text-align: center;
font-size: 12px;
line-height: 1.3;
color: #333333;
font-weight: bold;
border-bottom: 1px solid #eeeeee;
}
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" bgcolor="#222222" style="margin:0; padding:0; -webkit-text-size-adjust:none; -ms-text-size-adjust:none;">
<table cellpadding="0" cellspacing="0" border="0" height="100%" width="100%" bgcolor="#222222"
style="border-collapse:collapse;">
<tr>
<td>
<!-- Beginning of Outlook-specific wrapper : BEGIN -->
<!--[if (gte mso 9)|(IE)]>
<table width="1000" align="center" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<![endif]-->
<!-- Beginning of Outlook-specific wrapper : END -->
<!-- Email wrapper : BEGIN -->
<table border="0" width="100%" cellpadding="0" cellspacing="0" align="center"
style="max-width:960px; margin:auto;" class="email-container">
<tbody>
<tr>
<td>
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td valign="middle"
style="padding:10px 0; padding-left: 2%; text-align:left; font-family: 'Microsoft YaHei', sans-serif; font-size: 16px; color: #d6d6d6;"
width="135">
${reportNamespaceDesc}
</td>
<td valign="middle"
style="padding:10px 0; padding-right: 2%; text-align:right; line-height:1.1; font-family: 'Microsoft YaHei', sans-serif; font-size: 16px; color: #d6d6d6;">
${reportDate}
</td>
</tr>
</tbody>
</table>
<table border="0" width="100%" cellpadding="0" cellspacing="0" bgcolor="#222222"
style="padding:2%">
<tbody>
<tr>
<td align="center"
style="padding-top:5%; padding-bottom:0; font-family: 'Microsoft YaHei', sans-serif; font-size: 24px; line-height: 1.3; color: #222;"></td>
</tr>
</tbody>
</table>
<table border="0" width="100%" cellpadding="0" cellspacing="0" bgcolor="#ffffff"
style="padding:2%">
<tbody>
<tr>
<td align="center"
style="padding-top:4%; padding-bottom:0; font-family: 'Microsoft YaHei', sans-serif; font-size: 26px; font-weight:600; line-height: 1.3; color: #444444;">
${reportTitle}
</td>
</tr>
</tbody>
</table>
<!-- Service 統(tǒng)計數(shù)據(jù) BEGIN-->
<#list reportTableList as reportTable>
<table border="0" width="100%" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
<tbody>
<tr>
<td style="padding-left: 4%; padding-top: 2%; padding-bottom: 0; font-family:'Microsoft YaHei', sans-serif; font-size: 16px; line-height: 1.3; color: #666666;">
${reportTable.dimensionName}:
<span style="color: #ff1d33;font-weight: bold;">${reportTable.dimension}</span>
</td>
</tr>
<tr>
<td style="padding-left: 4%; padding-top: 2%; padding-bottom: 0; font-family:'Microsoft YaHei', sans-serif; font-size: 16px; line-height: 1.3; color: #666666;">
說明:統(tǒng)計周期:最近一天冷蚂,統(tǒng)計方式:${reportTable.statistics}
</td>
</tr>
<tr>
<td style="padding: 4%; padding-top: 2%; padding-bottom: 2%">
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="">
<tbody>
<!-- 服務(wù)報表 title BEGIN -->
<tr>
<#list reportTable.serviceDataList as serviceData>
<td valign="top" align="left"
style="padding: 10px 0;font-family: 'Microsoft YaHei', sans-serif; text-align: center; font-size: 12px; line-height: 1.3; color: #333333; font-weight: bold; border-bottom: 1px solid #cccccc;">
${serviceData.metricName}(${serviceData.metricUnit})
</td>
</#list>
</tr>
<!-- 服務(wù)報表 title END -->
<!-- 服務(wù)報表 數(shù)據(jù) BEGIN-->
<tr>
<#list reportTable.serviceDataList as serviceData>
<td valign="top" align="left"
style="padding: 10px 0;font-family: 'Microsoft YaHei', sans-serif; text-align: center; font-size: 12px; line-height: 1.3; color: #333333; font-weight: bold; border-bottom: 1px solid #eeeeee;">
${serviceData.metricValue}
</td>
</#list>
</tr>
<!-- 服務(wù)報表 數(shù)據(jù) END-->
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</#list>
<!-- Service 統(tǒng)計數(shù)據(jù) END-->
</td>
</tr>
<!-- Footer : BEGIN -->
<tr>
<td style="text-align:center; padding:4% 0; font-family:'Microsoft YaHei', sans-serif; font-size:13px; line-height:1.2; color:#666666;">
收到該郵件是因為您在 XXX上訂閱了 XXX 的相關(guān)數(shù)據(jù),可登錄 XXX 更新訂閱配置汛闸。
</td>
</tr>
<!-- Footer : END -->
</tbody>
</table>
<!-- Email wrapper : END -->
</td>
</tr>
</table>
</body>
</html>
3.3 編寫模板渲染工具類
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
/**
* 郵件模板生成工具類
*/
public class TemplateUtil {
/**
*
* @param ftl FTL文件地址
* @param params 填充參數(shù)
* @return
* @throws IOException
* @throws TemplateException
*/
public static String generateTemplate(String ftl, Map<String, Object> params)
throws IOException, TemplateException {
Configuration configuration = new Configuration();
configuration.setClassForTemplateLoading(TemplateUtil.class, "/");
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Template template = configuration.getTemplate(ftl);
Writer stringWriter = new StringWriter();
template.process(params, stringWriter);
return stringWriter.toString();
}
}
3.4 編寫定時任務(wù)
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@Service
public class DailyReportTask extends Task {
private static final Logger logger = Logger.getLogger(DailyReportTask.class);
//TFL模板文件的相對位置
private static final String FTL_FILE = "report/report.ftl";
private static final Integer RECENT_HOURS = 24;
private static final String TIME_PATTERN = "HH:mm";
private static final String DATE_PATTERN = "yyyy年MM月dd日";
private static final FastDateFormat TIME_FORMAT = FastDateFormat.getInstance(TIME_PATTERN, TimeZone.getTimeZone("Asia/Shanghai"));
@Override
public Set<String> generateShards() {
return getSingleShard();
}
// 報表發(fā)送任務(wù)的線程池
private ExecutorService executorService = new ThreadPoolExecutor(32, 32, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(512), new ThreadFactory() {
private AtomicInteger count = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(new ThreadGroup("cm-task"), r, "daily-report-send-"
+ count.getAndIncrement());
return thread;
}
}, new ThreadPoolExecutor.DiscardPolicy());
/**
* 報表
*/
public class ReportTable {
String dimensionName;
String dimension;
String statistics;
List<ServiceData> serviceDataList;
public String getDimensionName() {
return dimensionName;
}
public void setDimensionName(String dimensionName) {
this.dimensionName = dimensionName;
}
public String getDimension() {
return dimension;
}
public void setDimension(String dimension) {
this.dimension = dimension;
}
public String getStatistics() {
return statistics;
}
public void setStatistics(String statistics) {
this.statistics = statistics;
}
public List<ServiceData> getServiceDataList() {
return serviceDataList;
}
public void setServiceDataList(List<ServiceData> serviceDataList) {
this.serviceDataList = serviceDataList;
}
}
/**
* 報表中的數(shù)據(jù)
*/
public class ServiceData {
String metricName;
String metricUnit;
Double metricValue;
public String getMetricName() {
return metricName;
}
public void setMetricName(String metricName) {
this.metricName = metricName;
}
public String getMetricUnit() {
return metricUnit;
}
public void setMetricUnit(String metricUnit) {
this.metricUnit = metricUnit;
}
public Double getMetricValue() {
return metricValue;
}
public void setMetricValue(Double metricValue) {
this.metricValue = metricValue;
}
}
@Override
public boolean process(String taskShard) {
try {
List<ReportSetting> reportSettingList = reportSettingService.getReportSettingList();
// 沒有任務(wù)則直接返回
if (reportSettingList.size() == 0) {
return true;
}
logger.debug("reportSettingList size=" + reportSettingList.size());
for (ReportSetting reportSetting : reportSettingList) {
String projectId = reportSetting.getProjectId();
String sendTime = reportSetting.getSendTime();
String title = reportSetting.getTitle();
String namespace = reportSetting.getNamespace();
List<String> reportEmailList = reportSetting.getReportEmailList();
ReportTemplete reportTemplete = reportTempleteService.getReportTemplete(reportSetting.getTempletId());
String statistics = reportTemplete.getStatistics();
// 檢查是否到報表發(fā)送時間
if (checkSend(sendTime)) {
String[] metricNameKeyArray = reportTemplete.getMetricNameIdList().split(",");
FastDateFormat dateFormat = FastDateFormat.getInstance(DATE_PATTERN, TimeZone.getTimeZone("Asia/Shanghai"));
// 定義一封郵件的參數(shù)
Map<String, Object> params = new HashMap<>();
params.put("reportNamespaceDesc", descService.getNamespaceDesc(namespace));
params.put("reportTitle", title);
params.put("reportDate", dateFormat.format(new Date()));
List<ReportTable> reportTableList = new ArrayList<>();
String dimensionName;
if (metricNameKeyArray.length > 0) {
dimensionName = metricNameKeyArray[0].split("##")[2];
} else {
continue;
}
// 獲取用戶的實例資源列表
List<String> dimensionList = metricService.getActiveDimensionList(projectId, namespace, dimensionName, RECENT_HOURS);
if (dimensionList.size() == 0) {
continue;
}
// 每個實例標識一張表
for (String dimension : dimensionList) {
ReportTable reportTable = new ReportTable();
reportTable.setDimensionName("");
reportTable.setDimension(dimension);
reportTable.setStatistics(statistics);
List<ServiceData> serviceDataList = new ArrayList<>();
for (String metricNameKey : metricNameKeyArray) {
// 獲取監(jiān)控項的描述蝙茶、單位和數(shù)值
String metricNameDesc = "XXX";
String unitDesc = "XXX";
Double dimensionValue = 12.0;
ServiceData serviceData = new ServiceData();
serviceData.setMetricName(metricNameDesc);
serviceData.setMetricUnit(unitDesc);
serviceData.setMetricValue(dimensionValue);
serviceDataList.add(serviceData);
}
reportTable.setServiceDataList(serviceDataList);
reportTableList.add(reportTable);
}
params.put("reportTableList", reportTableList);
// 利用freemaker模板生成報表郵件內(nèi)容
String htmlContent = TemplateUtil.generateTemplate(FTL_FILE, params);
logger.info("htmlContent:" + htmlContent);
// 發(fā)送郵件
for (String email : reportEmailList) {
// 改順序發(fā)送為線程池調(diào)度發(fā)送
// boolean res = EmailUtils.SendEmail(email, title, htmlContent);
// if (!res){
// logger.error("郵件發(fā)送失敗");
// }
executorService.submit(new EmailSendThread(email, title, htmlContent));
}
}
}
return true;
} catch (Exception e) {
return false;
}
}
/**
* 檢查是否需要發(fā)送報表
*
* @param sendTime
* @return
*/
private boolean checkSend(String sendTime) {
//略去部分細節(jié)
}
}
4.5 測試結(jié)果
收到的測試郵件內(nèi)容如下圖所示:
五、其他
郵件發(fā)送的內(nèi)容需要用到郵件服務(wù)器以及編寫郵件發(fā)送工具類诸老,這一細節(jié)不是本文的重點隆夯,后面考慮通過另外一篇文章單獨進行介紹,這里忽略别伏。
后續(xù)文章可能還會考慮介紹另外一種同樣比較常用的模板渲染引擎Velocity
吮廉,敬請期待。