Java模板引擎之 FreeMarker

摘要: 在Java模板引擎 FreeMarker介紹中,我們已經對freemarker有了一定的瞭解,這一節是FreeMarker入門教程。下載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

你好:${username}

02.ftl

複製代碼
<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工具類

這個類,主要是獲取模板定義文件,並根據輸入的數據輸出到控制檯和文件中

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();
        }
    }
    }
}

  模擬數據,進行測試

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");
    }
}

下載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

你好:${username}

02.ftl

複製代碼
<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工具類

這個類,主要是獲取模板定義文件,並根據輸入的數據輸出到控制檯和文件中

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();
        }
    }
    }
}

  模擬數據,進行測試

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");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章