2010-02-15 38 views
0

我是freemarker的新手。我有一個我準備使用freemarker的spring應用程序。模板將存儲在數據庫中,並基於登錄名,我想從數據庫中檢索模板。任何人都可以告訴我如何在spring中配置freemarker,並在構造模板之後將html標籤作爲字符串獲取。我做了谷歌搜索,但我不明白。使用freemarker和spring構建模板

我嘗試了這個級別。在春天我已經做到了這個級別。最後我想要一個字符串中的html標籤。

// Spring freemarker specific code 
Configuration configuration = freemarkerConfig.getConfiguration(); 
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); 
// My application specific code 
String temp = tempLoader.getTemplateForCurrentLogin(); 

謝謝。

回答

2

爲了配合你貼的代碼位在一起,你可以做這樣的事情:

// you already have this bit 
String templateText = tempLoader.getTemplateForCurrentLogin(); 

// now programmatically instantiate a template 
Template t = new Template("t", new StringReader(templateText), new Configuration()); 

// now use the Spring utility class to process it into a string 
// myData is your data model 
String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, myData);