Freemarker静态化加载模板的三种方式
Freemarker 提供了三种加载模板目录的方法。 它使用Configuration
类加载模板
三种方法分别是:
1.public void setDirectoryForTemplateLoading(File dir) throws IOException;
2.public void setClassForTemplateLoading(Class clazz, String pathPrefix);
3.public void setServletContextForTemplateLoading(Object servletContext, String path);
第一种:以文件的绝对路径加载
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("/opt/user/template"));
第二种:以类文件的路径加载
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);
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/152.html
转载时必须以链接形式注明原始出处及本声明。
本文主题:
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。