一般使用freemarker读取模板文件都是从文件系统中读取的,指定一下目录和文件名,就可以得到template对象,代码的写法就像:
cfg=new Configuration();
cfg.setDirectoryForTemplateLoading("d:\\");
Template t = cfg.getTemplate("1373");
如果想要实现从数据库中读取就需要实现TemplateLoader接口,并把它传递给Configuration对象,TemplateLoader有四个方法。
//关闭模板源
public void closeTemplateSource(Object templateSource) throws IOException;
//查找模板源,也可以理解为打卡一个模板源
public Object findTemplateSource(String param) throws IOException;
//得到最后一次修改模板的时间,不知道什么时间修改的就传-1
public long getLastModified(Object templateSource);
//得到template对象的reader
public Reader getReader(Object templateSource, String encodeType) throws IOException;
重新实现templateloader,将其传递给Configuration后的完整代码:
cfg=new Configuration();
cfg.setTemplateLoader(new TemplateLoader(){
Con