FreeMarker是一個用Java語言編寫的模板引擎,它基于模板來生成文本輸出。
FreeMarker與Web容器無關,即在Web運行時,它并不知道Servlet或HTTP。
它不僅可以用作表現層的實現技術,而且還可以用于生成XML,JSP或Java 等。
Demo:
一、在eclipse中創建FreeMarker模板
${user.userName}
${user.userPassword}
并將其命名為“user.ftl”${user.userPassword}
二、在eclipse中創建FreeMarker數據模型
以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
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數據模型不是文本文件,而是樹狀結構的。
三、在eclipse中填充FreeMarker數據模型
將創建好的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);
user.setUserName("測試");
user.setUserPassword("123");
Map<String, Object> root = new HashMap<String, Object>();
root.put("user", user);
四、創建FreeMarker的模板引擎,解析模板
1.創建和配置Configuration對象,Configuration對象實例負責管理FreeMarker模板的路徑加載及模板的創建和緩存。
通常應用程序的生命周期中只會創建一個Configuration實例。
2.獲取模板實例,即通過Configuration實例獲取Template實例,調用getTemplate()方法。
3.合并數據模型和模板
該步驟完整代碼:
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 * 數據模型根對象
30 */
31 public static void analysisTemplate(String templateName,
32 String templateEncoding, Map<?, ?> root) {
33 try {
34 /**
35 * 創建Configuration對象
36 */
37 Configuration config = new Configuration();
38 /**
39 * 指定模板路徑
40 */
41 File file = new File("templates");
42 /**
43 * 設置要解析的模板所在的目錄,并加載模板文件
44 */
45 config.setDirectoryForTemplateLoading(file);
46 /**
47 * 設置包裝器,并將對象包裝為數據模型
48 */
49 config.setObjectWrapper(new DefaultObjectWrapper());
50
51 /**
52 * 獲取模板,并設置編碼方式,這個編碼必須要與頁面中的編碼格式一致
53 */
54 Template template = config.getTemplate(templateName,
55 templateEncoding);
56 /**
57 * 合并數據模型與模板
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
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 * 數據模型根對象
30 */
31 public static void analysisTemplate(String templateName,
32 String templateEncoding, Map<?, ?> root) {
33 try {
34 /**
35 * 創建Configuration對象
36 */
37 Configuration config = new Configuration();
38 /**
39 * 指定模板路徑
40 */
41 File file = new File("templates");
42 /**
43 * 設置要解析的模板所在的目錄,并加載模板文件
44 */
45 config.setDirectoryForTemplateLoading(file);
46 /**
47 * 設置包裝器,并將對象包裝為數據模型
48 */
49 config.setObjectWrapper(new DefaultObjectWrapper());
50
51 /**
52 * 獲取模板,并設置編碼方式,這個編碼必須要與頁面中的編碼格式一致
53 */
54 Template template = config.getTemplate(templateName,
55 templateEncoding);
56 /**
57 * 合并數據模型與模板
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