作为一个模板框架,freemarker的功能还是很强大的。在模板处理方面,freemarker有多种形式,最常见的方式是将模板文件放在一个统一的文件夹下面,如下形式:
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("templates"));
如果我想把模板存放到数据库中,可以实现吗?答案是肯定的。在这里可以使用StringTemplateLoader来加载模板内容。主要的代码实现如下所示:
Configuration cfg = new Configuration(); 
StringTemplateLoader stringLoader = new StringTemplateLoader();  
String templateContent="hello ${name}!";  
stringLoader.putTemplate("myTemplate",templateContent);  
cfg.setTemplateLoader(stringLoader);  
Template template = cfg.getTemplate("myTemplate","utf-8");

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
  • 2022-02-21
  • 2022-01-07
  • 2021-11-08
  • 2021-11-06
猜你喜欢
  • 2022-02-09
  • 2021-11-20
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
相关资源
相似解决方案