我想從2個不同的地方加載Freemarker的模板。其中FTL文件所在的文件夾是:Freemarker:從不同位置加載模板
/docroot/WEB-INF/src/charts/base
/docroot/WEB-INF/src/charts/tmpl
我有下面的類有2種方法:
public class TemplateLoader {
public Template loadChartBaseTemplate(String templateName) throws Exception {
String templateFile = String.format("%s.ftl", templateName);
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(this.getClass(), "/charts/base/");
Template template = cfg.getTemplate(templateFile);
return template;
}
public Template loadChartTemplate(String fileName) throws Exception {
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(this.getClass(), "/charts/tmpl/");
Template template = cfg.getTemplate(fileName);
return template;
}
}
第一種方法正確加載模板。第二種方法返回一個錯誤,指出「找不到名稱的模板」。兩個文件夾都處於同一級別,並且檢查了我嘗試加載的文件是否存在。
什麼可能會導致Freemarker能夠從一個文件夾中加載模板,併爲另一個文件夾失敗,因爲它們處於同一級別(我嘗試了結尾「/」,沒有它,它沒有區別,行爲是一樣的)。
一個額外的問題:
以下行返回錯誤:構造配置()已過時
Configuration cfg = new Configuration();
所有的文檔和例子我見過有像這樣。有沒有其他的方式來做到這一點?
在此先感謝您的幫助。
感謝您的幫助。我只是想知道這裏發生了什麼。我正在使用Liferay portlet。項目中添加了「基礎」中的模板。 「tmpl」文件夾是空的,並且在那裏的ftl文件是在運行時創建的。會發生什麼情況是,調試器正在查找源代碼文件夾(Eclipse Workspace)中的文件,其中「tmpl」文件夾爲空;我在運行時創建的文件正在部署文件夾(Liferay的Apache webapps文件夾)中創建。總之,調試器正在錯誤的位置查找ftl文件。 – jgrodrigueza