在我的应用程序中,所有freemarker模板都在/ templates/ftl/so中,在应用程序部署期间,我加载了一个类,我调用了一个扩展FreemarkerManager的类,并且有一个方法
Configuration configuration = super.createConfiguration(servletContext);
configuration.setDirectoryForTemplateLoading(new File("/templates/ftl/"));
Run Code Online (Sandbox Code Playgroud)
这样,当我需要加载模板文件时,我可以这样做:
ServletContext servletContext = ServletActionContext.getServletContext();
Configuration configFreemarker = (Configuration) servletContext
.getAttribute("freemarker.Configuration");
Template template = configFreemarker.getTemplate("pathToMyTemplate");
Run Code Online (Sandbox Code Playgroud)
在一个特定情况下,我需要获得一个来自完全不同路径的模板(而不是/ templates/ftl /).
我怎样才能在这个特定的时刻声明第二个目录进行模板加载而不会破坏调用旧路径的所有现有代码?我可以同时为模板加载提供2个不同的起点吗?
谢谢
你可以用MultipleTemplateLoader.
import freemarker.cache.*; // template loaders live in this package
...
FileTemplateLoader ftl1 = new FileTemplateLoader(new File("/tmp/templates"));
FileTemplateLoader ftl2 = new FileTemplateLoader(new File("/usr/data/templates"));
ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(), "");
TemplateLoader[] loaders = new TemplateLoader[] { ftl1, ftl2, ctl };
MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
cfg.setTemplateLoader(mtl);
Run Code Online (Sandbox Code Playgroud)
资料来源:Freemarker手册
| 归档时间: |
|
| 查看次数: |
5731 次 |
| 最近记录: |