FreeMarker快速上手
創(chuàng)建Configuration實(shí)例
首先必須創(chuàng)建一個(gè)freemarker.template.Configuration 實(shí)例并調(diào)整其設(shè)置。Configuration 實(shí)例保存freemarker的設(shè)置,同時(shí)處理預(yù)解析的模板的創(chuàng)建和緩存。
通常應(yīng)用程序的生命周期中只會(huì)創(chuàng)建一個(gè)Configuration實(shí)例。
Configuration cfg = new Configuration(); // 指定模板文件的數(shù)據(jù)源,這里是一個(gè)文件目錄。cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates")); // 指定模板如何發(fā)現(xiàn)數(shù)據(jù)模型,這是一個(gè)高級(jí)主題,暫且這樣使用。 cfg.setObjectWrapper(new DefaultObjectWrapper());?
目前我們使用單個(gè)的Configuration實(shí)例。不過(guò)如果一個(gè)系統(tǒng)有多個(gè)獨(dú)立的組件使用FreeMarker,它們會(huì)使用各自的Configuration 實(shí)例。?
創(chuàng)建數(shù)據(jù)模型
我們可以簡(jiǎn)單地使用java.lang java.util 和自定義的JavaBean構(gòu)建對(duì)象模型,比如我們構(gòu)建數(shù)據(jù)模型如下:?
?
(root)|+- user = "Big Joe"|+- latestProduct|+- url = "products/greenmouse.html"|+- name = "green mouse"?
如下是構(gòu)建數(shù)據(jù)模型的代碼:
// Create the root hash Map root = new HashMap(); // Put string ``user'' into the root root.put("user", "Big Joe"); // Create the hash for ``latestProduct'' Map latest = new HashMap(); // and put it into the root root.put("latestProduct", latest); // put ``url'' and ``name'' into latest latest.put("url", "products/greenmouse.html"); latest.put("name", "green mouse");?
也可以使用一個(gè)包含url 和 name 屬性的JavaBean實(shí)例表示lastestProduct。
獲取模板
模板通過(guò)freemarker.template.Template實(shí)例表示。通常從Configuration 實(shí)例中獲取Template實(shí)例,任何時(shí)候都可以調(diào)用getTemplate方法獲取一個(gè)Template 實(shí)例。假定模板文件test.ftl 保存在先前設(shè)置的目錄中:
Template temp = cfg.getTemplate("test.ftl");?
上述代碼將讀取,解析/where/you/store/templates/test.ftl文件,創(chuàng)建一個(gè)對(duì)應(yīng)的Template實(shí)例 。
Configuration 緩存Template 實(shí)例, 因此當(dāng)需要再次獲取test.ftl 文件, 將不會(huì)創(chuàng)建一個(gè)新的Template實(shí)例。
合并模板和數(shù)據(jù)模型
就我們所知,數(shù)據(jù)模型+模板=輸出,通過(guò)調(diào)用模板的process 方法合并數(shù)據(jù)模型和模板,process. 方法接受一個(gè)數(shù)據(jù)模型根和一個(gè)writer作為參數(shù),將結(jié)果輸出到Writer。 為簡(jiǎn)化起見(jiàn),這里輸出到控制臺(tái)。
Writer out = new OutputStreamWriter(System.out); temp.process(root, out); out.flush();?
一旦獲取一個(gè)Template 實(shí)例,可以合并不同的數(shù)據(jù)模型和一個(gè)模板(Template 實(shí)例基本上是無(wú)狀態(tài)的),而test.ftl只會(huì)當(dāng)Template 實(shí)例被創(chuàng)建的時(shí)候訪問(wèn)一次。
?
當(dāng)然這里的out可以為文件,可以是XML、java等你想要的任何文件類(lèi)型,這樣就實(shí)現(xiàn)在代碼的生成.
整合
這是先前代碼片斷的源文件,不要忘記將freemarker.jar放在CLASSPATH.中。
import freemarker.template.*; import java.util.*; import java.io.*;public class Test {public static void main(String[] args) throws Exception {/* ------------------------------------------------------------------- */ /* You usually do it only once in the whole application life-cycle: */ /* Create and adjust the configuration */Configuration cfg = new Configuration();cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));cfg.setObjectWrapper(new DefaultObjectWrapper());/* ------------------------------------------------------------------- */ /* You usually do these for many times in the application life-cycle: */ /* Get or create a template */Template temp = cfg.getTemplate("test.ftl");/* Create a data model */Map root = new HashMap();root.put("user", "Big Joe");Map latest = new HashMap();root.put("latestProduct", latest);latest.put("url", "products/greenmouse.html");latest.put("name", "green mouse");/* Merge data model with template */Writer out = new OutputStreamWriter(System.out);temp.process(root, out);out.flush();} }?
總結(jié)
以上是生活随笔為你收集整理的FreeMarker快速上手的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: FreeMarker Eclipse P
- 下一篇: ldap的shema