2010-02-15 105 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);