jar包地址:
http://www.freemarker.org/freemarkerdownload.html
FreeMarker是一個用Java語言編寫的模板引擎,它基于模板來生成文本輸出。
FreeMarker與Web容器無關(guān),即在Web運(yùn)行時,它并不知道Servlet或HTTP。
它不僅可以用作表現(xiàn)層的實現(xiàn)技術(shù),而且還可以用于生成XML,JSP或Java 等。
Demo:
一、在eclipse中創(chuàng)建FreeMarker模板
${user.userName}
${user.userPassword}
并將其命名為“user.ftl”
二、在eclipse中創(chuàng)建FreeMarker數(shù)據(jù)模型
以User為例:
1 package test.client;
2
3 /**
4 * 用戶實體類
5 *
6 * @author Ying-er
7 * @time 2010-2-6下午04:05:25
8 * @version 1.0
9 */
10 public class User {
11 private String userName;
12
13 private String userPassword;
14
15 public String getUserName() {
16 return userName;
17 }
18
19 public void setUserName(String userName) {
20 this.userName = userName;
21 }
22
23 public String getUserPassword() {
24 return userPassword;
25 }
26
27 public void setUserPassword(String userPassword) {
28 this.userPassword = userPassword;
29 }
30
31 }
32
注意:注意:FreeMarker數(shù)據(jù)模型不是文本文件,而是樹狀結(jié)構(gòu)的。
三、在eclipse中填充FreeMarker數(shù)據(jù)模型
將創(chuàng)建好的User對象以key-value的形式封裝到Map中
片段代碼:
User user = new User();
user.setUserName("測試");
user.setUserPassword("123");
Map<String, Object> root = new HashMap<String, Object>();
root.put("user", user);
四、創(chuàng)建FreeMarker的模板引擎,解析模板
1.創(chuàng)建和配置Configuration對象,Configuration對象實例負(fù)責(zé)管理FreeMarker模板的路徑加載及模板的創(chuàng)建和緩存。
通常應(yīng)用程序的生命周期中只會創(chuàng)建一個Configuration實例。
2.獲取模板實例,即通過Configuration實例獲取Template實例,調(diào)用getTemplate()方法。
3.合并數(shù)據(jù)模型和模板
該步驟完整代碼:
1 package test.freemarker.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.OutputStreamWriter;
6 import java.io.Writer;
7 import java.util.Map;
8
9 import freemarker.template.Configuration;
10 import freemarker.template.DefaultObjectWrapper;
11 import freemarker.template.Template;
12 import freemarker.template.TemplateException;
13
14 /**
15 * freemarker 模板工具
16 *
17 * @author Ying-er
18 * @time 2010-2-6下午04:07:27
19 * @version 1.0
20 */
21 public class FreeMarkertUtil {
22 /**
23 *
24 * @param templateName
25 * 模板文件名稱
26 * @param templateEncoding
27 * 模板文件的編碼方式
28 * @param root
29 * 數(shù)據(jù)模型根對象
30 */
31 public static void analysisTemplate(String templateName,
32 String templateEncoding, Map<?, ?> root) {
33 try {
34 /**
35 * 創(chuàng)建Configuration對象
36 */
37 Configuration config = new Configuration();
38 /**
39 * 指定模板路徑
40 */
41 File file = new File("templates");
42 /**
43 * 設(shè)置要解析的模板所在的目錄,并加載模板文件
44 */
45 config.setDirectoryForTemplateLoading(file);
46 /**
47 * 設(shè)置包裝器,并將對象包裝為數(shù)據(jù)模型
48 */
49 config.setObjectWrapper(new DefaultObjectWrapper());
50
51 /**
52 * 獲取模板,并設(shè)置編碼方式,這個編碼必須要與頁面中的編碼格式一致
53 */
54 Template template = config.getTemplate(templateName,
55 templateEncoding);
56 /**
57 * 合并數(shù)據(jù)模型與模板
58 */
59 Writer out = new OutputStreamWriter(System.out);
60 template.process(root, out);
61 out.flush();
62 out.close();
63 } catch (IOException e) {
64 e.printStackTrace();
65 } catch (TemplateException e) {
66 e.printStackTrace();
67 }
68
69 }
70 }
71
posted on 2010-02-06 16:42
Ying-er 閱讀(25122)
評論(8) 編輯 收藏