2009年9月27日 星期日

Velocity - 使用備忘錄


Velocity - 使用備忘錄
Velocity 是拿來作模板套版使用 , 你可以把常用的某些內容透過模板定義的方式 , 由它幫你轉換
直接來看主程式
以下範例 , 是假設你要寄送一堆電子帳單給客戶 , 但是有可能電子帳單的內容會調來調去
(User 特別是User 的主管 , 總是會在最後一刻改變心意 , 告訴你 , 某段的內容要換掉某個字 , 甚至是標點符號的全形半形都會要你改)

以下是使用講解 , 只把重點挑出來 , 完整程式碼 , 請看最下方有附上
完成的測試範例包含
(1)
TestMainApp1.java
(2)
example1.vm
(3)
example1_big5.vm

以下針對
TestMainApp1.java 作重點講解
假設你有一個method , 會根據你傳入的資料,去進行套版 , 然後回傳套表之後的結果

public static String getMailVM(Properties pData) throws Exception{
String s1=null;
VelocityEngine ve=new VelocityEngine();

Properties p = new Properties();
//以下是告訴套表程式 , 你的套版檔案放在哪個目錄

p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "/home/demo/wk_myDemoLab/MyDemoVelocity/src");
//p.setProperty("runtime.log", "velocity.log");
ve.init(p);
//這個是放我們傳入的變數內容

VelocityContext context = new VelocityContext(pData);

Template template = null;

//以下是抓取套檔案 , 且要用哪種編碼方式讀取
//注意 , 如果你的套版檔案存檔時是用UTF8編碼 , 請用UTF8讀取
//注意 , 如果你的套版檔案存檔時是用big5編碼 , 請用Big5讀取
//不然就會造成 , 顯示時有可能只有變數會正常顯示 , 其他部份都是亂碼
// template = ve.getTemplate("example1.vm","UTF8");
template = ve.getTemplate("example1_big5.vm","big5");

StringWriter writer=new StringWriter();
//合併套版及變數 ,

if ( template != null) template.merge(context, writer);
writer.flush();
writer.close();
//將套版結果轉出成字串

s1=writer.toString();
ve=null;
context=null;
template =null;
return s1;
}


public static void main(String[] args) throws Exception {
//設定會給套版使用的變數 , 需要跟套版檔案配對

Properties p1=new Properties();
p1.setProperty("cust_name", "錢多多");
p1.setProperty("bank_name", "大台北郵局");
p1.setProperty("account_num", "200-123-456");
p1.setProperty("yr", "98");
p1.setProperty("mn", "06");
p1.setProperty("dy", "15");
String s1= getMailVM(p1);
System.out.println(s1);
}

}
======執行結果 , 粗體部分是我們傳入的變數=========
親愛的 錢多多 ,您好:
您的帳單將於980615日自您的大台北郵局200-123-456進行扣款。

客戶服務部
======================================
前面提到User 或是User的主管總是很多變 , 如果哪一天User 又想要變變花樣
把電子帳單的訊息改變甚至是要由 Text 變成HTML 怎麼辦?
在沒有增加變數的情況下 , 你只要打開 example.vm然後根據User要的 , 重新調整這個套版就可以了
完成不需要打開原始程式碼重新修改編譯....

把會變的部份 , 盡可能抽離出來 , 讓"修改"遠離你的原始程式碼,

以下是完整程式碼範例

=====example1.vm , 注意是要存檔為 UTF8編碼==================
親愛的 $cust_name ,您好:
您的帳單將於$yr年$mn月$dy日自您的$bank_name$account_num進行扣款。

客戶服務部

=========================================================
=====example1_big5.vm , 注意是要存檔為 Big5編碼=============

親愛的 $cust_name ,您好:
您的帳單將於$yr年$mn月$dy日自您的$bank_name$account_num進行扣款。

客戶服務部

==================================================================
TestMainApp1.java
==================================================================

package com.demo.velocity.examples;

import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

public class TestMainApp1 {
public static String getMailVM(Properties pData) throws Exception{
String s1=null;
VelocityEngine ve=new VelocityEngine();

Properties p = new Properties();
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "/home/demo/wk_myDemoLab/MyDemoVelocity/src");
//p.setProperty("runtime.log", "velocity.log");
ve.init(p);
VelocityContext context = new VelocityContext(pData);

Template template = null;
// template = ve.getTemplate("example1.vm","UTF8");
template = ve.getTemplate("example1_big5.vm","big5");

StringWriter writer=new StringWriter();
if ( template != null) template.merge(context, writer);
writer.flush();
writer.close();
s1=writer.toString();
ve=null;
context=null;
template =null;
return s1;
}
public static void main(String[] args) throws Exception {
Properties p1=new Properties();
p1.setProperty("cust_name", "錢多多");
p1.setProperty("bank_name", "大台北郵局");
p1.setProperty("account_num", "200-123-456");
p1.setProperty("yr", "98");
p1.setProperty("mn", "06");
p1.setProperty("dy", "15");
String s1= getMailVM(p1);
System.out.println(s1);
}

}

1 則留言:

farnallesacks 提到...

The 7 Best Casinos in San Diego - MapyRO
The 7 영천 출장샵 Best Casinos in San 여수 출장샵 Diego · 세종특별자치 출장샵 Harrah's Rincon Casino · The Cosmopolitan · Golden Nugget · Harrah's 남원 출장마사지 Resort & Casino · LINQ 밀양 출장안마 Promenade · Caesars