Freemarker的加载模板目录

来源:互联网 发布:程序员联合开发王 编辑:程序博客网 时间:2024/05/16 09:58

本文主要讲的是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所在的包路径是:  org.foo.util

那么获得是模板文件就是  ${projectPath}/org/foo/util/template 。

 

这里注意一下第二个参数需要以 "/" 开头。

 

其实我们看下源码就知道了:

 

 

可以看到,prefix如果最后1个字符不是 "/" 会默认加上。 但是第一个不是 "/" 字符 并不会加上。

 

最终的具体生成代码是通过FMParser这个类生成的。  FMParser是使用JavaCC写的,由于时间有限,就没有具体研究了。

欢迎访问我的技术群425783133

0 0
原创粉丝点击