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

Java 文字產生器:用 Velocity 模板引擎實現邏輯與視圖分離

     編輯:Java 相關知識

痛點:字串拼接地獄

StringBuffer msg = new StringBuffer();
msg.append("親愛的 ").append(cust.getName()).append(":\n\n");
msg.append("您的帳戶餘額是 ").append(cust.getBalance()).append("\n\n");
msg.append("致禮!\n某某公司");
mail.send(cust.getEmail(), "帳單", msg.toString());

缺點:業務邏輯與文字耦合、非程式設計師無法維護、結構變更需改碼重編譯。

解決方案:模板引擎 + MVC

模型:資料物件。視圖:純文字模板。控制器:準備資料、合併模板、發送輸出。Velocity 是成熟、輕量、語法簡單的選擇。

Velocity 核心語法

語法說明
$variable輸出變數 toString(),找不到輸出原字面值
$obj.property自動呼叫 getProperty() 或直接存取欄位(反射)
#if ... #else #end條件判斷
#foreach($item in $list) ... #end迭代 Collection/Array/Iterator
#set($x = 1)賦值
#include("file")靜態包含
#parse("file")動態解析包含

Email 範例重構

模板 email.vm

親愛的 $customer.name:

您的帳戶餘額是 $customer.balance

致禮!
某某公司

#if($month == 12)
祝您聖誕快樂!
#end

控制器代碼

VelocityContext ctx = new VelocityContext();
ctx.put("customer", customer);           // 直接放入領域物件
ctx.put("month", Calendar.getInstance().get(Calendar.MONTH) + 1);

StringWriter writer = new StringWriter();
Velocity.mergeTemplate("email.vm", "UTF-8", ctx, writer);
mail.send(customer.getEmail(), "帳單", writer.toString());

Web 應用:Servlet + HTML 模板

Servlet

Template tmpl = Velocity.getTemplate("report.vm");
VelocityContext ctx = new VelocityContext();
ctx.put("customers", CustomerDAO.getAll());

response.setContentType("text/html;charset=UTF-8");
Velocity.mergeTemplate("report.vm", "UTF-8", ctx, response.getWriter());

report.vm

<html><body><h1>客戶報表</h1>
#foreach($c in $customers)
  <h2>$c.name</h2>
  <table>
    #foreach($tx in $c.transactions)
      <tr><td>$tx.date</td><td>$tx.desc</td><td>$tx.amount</td></tr>
    #end
    <tr><td colspan="2">總計</td><td><b>$c.total</b></td></tr>
  </table>
#end
</body></html>

效能優化:模板快取

Properties props = new Properties();
props.setProperty("file.resource.loader.cache", "true");
props.setProperty("file.resource.loader.modificationCheckInterval", "3600"); // 秒
Velocity.init(props);
  • 開發階段關閉快取,修改即時生效。
  • 生產環境開啟快取,間隔檢查更新(如 1 小時)。

進階用法

  • 自定義指令:實作 Directive 擴展標籤(如 #paginate)。
  • 工具箱:VelocityTools 提供 $date, $number, $link 等常用工具。
  • 替代品:FreeMarker(更強型別檢查)、Thymeleaf(Spring 整合佳)、JTE(編譯期型別安全)。

總結

模板引擎以「邏輯與視圖分離」解決文字生成維護難題。Velocity 語法極簡、學習曲線平緩,適合非技術人員協作。核心流程:Context.put(data) → Template.merge(context, writer)。生產環境務必啟用快取。現代專案可考慮 FreeMarker/Thymeleaf 獲得更好 IDE 支援與型別安全,但「模板化思想」永不過時。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved