Freemarker是一个模板框架,主要是为了加快染速度而产生的。它与web容器无关,只要是关于模板生成一些代码的都可以使用它完成。
比如xml,Java代码的生成等。 其他类似的模板框架还有velocity。
本文主要讲的是Freemarker的加载模板目录问题,它的语法就不描述了。具体的语法可在官网下载参考手册参考即可。
加载模板目录方法
Freemarker提供了3种加载模板目录的方法。 它使用Configuration类加载模板
3种方法分别是:
public void setClassForTemplateLoading(Class clazz, String pathPrefix);
public void setDirectoryForTemplateLoading(File dir) throws IOException;
public void setServletContextForTemplateLoading(Object servletContext, String path);
看名字也就知道了,分别基于类路径、文件系统以及Servlet Context。
第二,三种没啥好说的。
第二种基于文件系统。 比如加载/home/user/template下的模板文件。
Configuration cfg = new Configuration(); cfg.setDirectoryForTemplateLoading(new File("/home/user/template")); cfg.getTemplate("Base.ftl");
这样就获得了/home/user/template/Base.ftl这个模板文件。
第三种基于web project。 第二个参数是基于WebRoot下的。
比如: setServletContextForTemplateLoading(context, "/ftl") 就是 /WebRoot/ftl目录。
第一种基于类路径的方法有点小坑,其实看下源码代码就知道了。
比如 :
Configuration cfg = new Configuration(); cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/template"); cfg.getTemplate("Base.ftl");
其实这个方法是根据类加载路径来判断的,最终会执行以下代码:
FreemarkerUtil.class.getClassLoader().getResource("/template/");
这里注意一下第二个参数需要以 "/" 开头。
类加载模板文件示例:
/**
* @param pathPrefix classpath类路径下文件夹
* @param fileName 模板文件带扩展名
* @param parms 参数集合
* @return
* @throws Exception
*/
private String getClassForTemplate(String pathPrefix, String fileName, Map<String, Object> params) throws Exception {
Configuration config = new Configuration();
config.setSharedVariable("classic_compatible", true);
config.setClassForTemplateLoading(this.getClass(), pathPrefix);
config.setObjectWrapper(new DefaultObjectWrapper());
Template temp = config.getTemplate(fileName);
if (temp == null) {
return "";
}
StringWriter result = new StringWriter();
temp.process(params, result);
return result.toString();
}