Freemarker中Configuration的setClassForTemplateLoading方法参数问题

今天使用freemarker中Configuration的setClassForTemplateLoading方法遇到了加载模板目录的一个小问题。

由于网上的其他论坛,博客写的有点乱,故记录一下。

 

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/");

 

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

 

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

 

 

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

最后获得的URL方法:

我们可以看到URL是通过loaderClass.getResource(fullPath) 获得的。

 

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

参考资料

http://my.oschina.net/flashsword/blog/160747

posted on 2014-04-24 20:20  format丶  阅读(29017)  评论(0编辑  收藏  举报

导航