Freemarker提供了3种加载模板目录的方法。 它使用Configuration类加载模板
3种方法分别是:
public void setDirectoryForTemplateLoading(File dir) throws IOException;
public void setClassForTemplateLoading(Class clazz, String pathPrefix);
public void setServletContextForTemplateLoading(Object servletContext, String path);
第一种 以文件的绝对路径加载
<span style="font-size:14px;">Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("/opt/user/template"));</span>
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/template");第三种 基于WEB服务器的路径加载
Configuration cfg = new Configuration();
cfg.setServletContextForTemplateLoading(servletContext, "/user/template");
注意一下第二个参数需要以 "/" 开头
同时加载多个模块的模板
//系统模板路径
Configuration cfg = new Configuration();
WebappTemplateLoader wtl = new WebappTemplateLoader(servletContext,"");
WebappTemplateLoader wt2 = new WebappTemplateLoader(servletContext,"/"+SystemConstants.SITE_ROOT+"/"+siteCode);
TemplateLoader[] loaders = new TemplateLoader[] { wtl, wt2};
MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
cfg.setTemplateLoader(mtl);
本文介绍了Freemarker提供的三种加载模板目录的方法:通过文件绝对路径、类文件路径及基于WEB服务器路径进行模板加载,并展示了如何同时加载多个模块的模板。
608

被折叠的 条评论
为什么被折叠?



