Hackingwu's coding house
阿新 • • 發佈:2019-01-23
FreeMarker是一個模版引擎,通過模板+資料生成頁面,這個頁面可以使JSP,xml.....
ftl字尾名freemtemplate是他的模板檔案的字尾名。
FreeMarker不是一個Web應用框架,與容器無關。
一般模板檔案放在專案下的templates資料夾下。
寫FreeMarker 的Java程式碼分為以下四個步驟
(1)建立FreeMarker配置例項
(2)建立資料模型
(3)載入模板檔案
(4)顯示生成的資料
Configuration configuration = new Configuration();
//templates寫的是你的Freemarker的模板檔案的目錄相對於專案而言。
configuratioin.setDirectoryTemplateLoading(new File("templates"));
Map root = new HashMap();
root.put("user","hackingwu");
Template template = configuration.getTemplate("a.ftl");
//System.out是輸出到控制檯。檔案,socket......
Writer out = new OutputStreamWriter(System.out);
template.process(root,out);
out.flush();