首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2EE

FreeMarker實戰:高性能靜態頁生成最佳實踐(附完整工具類與模板)

     編輯:J2EE

應用場景

門戶型網站高頻讀取數據 → 頁面靜態化降低DB壓力;低變頻數據 → 緩存極致響應。本文演示FreeMarker生成靜態HTML完整方案。

核心工具類 FreeMarkerBuildHtmlFile

public class FreeMarkerBuildHtmlFile {
    public static void createPubMessageHtml(PublicMessage message, File saveDir) {
        try {
            if (!saveDir.exists()) saveDir.mkdirs();
            Configuration cfg = FreeMarkerManager.getConfiguration();
            Template template = cfg.getTemplate("message/messageView.ftl");
            
            FileOutputStream outStream = new FileOutputStream(new File(saveDir, message.getId() + ".html"));
            OutputStreamWriter writer = new OutputStreamWriter(outStream, "GBK");
            BufferedWriter sw = new BufferedWriter(writer);
            
            Map<String, PublicMessage> rootMap = new HashMap<>();
            rootMap.put("message", message);
            
            template.process(rootMap, sw);  // 模板引擎渲染
            sw.flush(); sw.close(); outStream.close();
        } catch (Exception e) { e.printStackTrace(); }
    }
}

class FreeMarkerManager {
    private static Configuration cfg = new Configuration();
    static {
        // 類路徑相對路徑加載模板
        cfg.setTemplateLoader(new ClassTemplateLoader(FreeMarkerManager.class, "/ftl"));
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        // 忽略空屬性異常,支援${a.b.c.d}鏈式安全訪問
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
    }
    public static Configuration getConfiguration() { return cfg; }
}

模板文件 messageView.ftl

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<style>
.tdEditTitle { font-size:12px; color:#003366; font-weight:bold; background:#E6F5FC; text-align:center; height:24px; }
.tableEdit { background-color:white; }
.MyButton { border:1px solid #707070; padding:2px; font-size:12px; cursor:hand; color:black; }
</style>
<title>公共信息明細</title>
</head>
<body>
<center>
<table class="tableEdit" border="0" cellspacing="1" cellpadding="0" style="width:580px;">
<tbody>
<tr>
    <td align="center" class="tdEditTitle">${message.title}</td>
</tr>
<tr>
    <td>
        <table class="tableEdit" style="width:580px;" cellspacing="0" border="0" cellpadding="0">
        <tr><td colspan="3" align="left">${message.content}</td></tr>
        </table>
    </td>
</tr>
</tbody>
</table>
<table>
<tr align="center">
    <td colspan="3" bgcolor="#EFF3F7">
        <input type="button" class="MyButton" value="關閉窗口" onclick="window.close()">
    </td>
</tr>
</table>
</center>
</body>
</html>

集成觸發點

在增/刪/改Action執行成功後,調用FreeMarkerBuildHtmlFile.createPubMessageHtml(message, saveDir),實現寫時更新、讀時直取,DB壓力歸零。

關鍵配置解析

  • ClassTemplateLoader:模板打包進JAR/CLASSPATH,部署遷移無憂
  • IGNORE_HANDLER:模板表達式容錯,避免空指針中斷渲染
  • GBK編碼:中文環境兼容性考量,現代項目建議統一UTF-8
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved