Java模板引擎FreeMarker系列之二helloword

HelloWord 吧 FreeMarker

下载freemarker

http://freemarker.sourceforge.net/freemarkerdownload.html

 

项目文件:freemarker-2.3.19.tar.gz

中文文档:FreeMarker_Manual_zh_CN.pdf

解压后把freemarker.jar加到classpath中即可完成环境的配置

 

定义模板文件

我们创建两个模板文件,一个是纯文本的模板文件,另一个是HTML格式的模板文件,主要是为了说明,freemarker的模板文件可以是任何格式的

01.ftl

1
你好:${username}

02.ftl

1
2
3
4
5
6
7
8
9
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${username}</h1>
</body>
</html>

 

创建freemarker工具类

这个类,主要是获取模板定义文件,并根据输入的数据输出到控制台和文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.naxsu.freemarker;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreemarkerUtil {
    /**
     * 获取模板
     * @param name
     * @return
     */
    public Template getTemplate(String name) {
    try {
        //通过Freemaker的Configuration读取相应的ftl
        Configuration cfg = new Configuration();
        //设定去哪里读取相应的ftl模板文件
        cfg.setClassForTemplateLoading(this.getClass(),"/ftl");
        //在模板文件目录中找到名称为name的文件
        Template temp = cfg.getTemplate(name);
        return temp;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
    }
    /**
     * 输出到控制台
     * @param name 模板文件名
     * @param root
     */
    public void print(String name,Map<String,Object> root) {
        try {
            //通过Template可以将模板文件输出到相应的流
        Template temp = this.getTemplate(name);
        temp.process(root, new PrintWriter(System.out));
    } catch (TemplateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
    /**
     * 输出到文件
     * @param name
     * @param root
     * @param outFile
     */
    public void fprint(String name,Map<String,
                       Object> root,String outFile) {
    FileWriter out = null;
    try {
            //通过一个文件输出流,就可以写到相应的文件中
        out = new FileWriter(
                      new File("E:\\freemarker\\ftl\\"+outFile));
        Template temp = this.getTemplate(name);
        temp.process(root, out);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TemplateException e) {
        e.printStackTrace();
    } finally {
        try {
        if(out!=null) out.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
    }
    }
}

 

模拟数据,进行测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class TestFreemarker {
    FreemarkerUtil fu;
    Map<String,Object> root = null;
    @Before
    public void setUp() {
        fu = new FreemarkerUtil();
        root = new HashMap<String,Object>();
    }
    @Test
    public void test01() {
        //1、创建数据模型
        Map<String,Object> root = new HashMap<String,Object>();
        //2、为数据模型添加值
        root.put("username", "张三");
        //3、将数据模型和模板组合的数据输出到控制台
        fu.print("01.ftl", root);
        fu.fprint("02.ftl", root, "01.html");
    }
}

好了,helloword到此为止,简单入门就是如此简单

posted @ 2014-11-13 22:26  韩慧兵  阅读(372)  评论(0编辑  收藏  举报