freemarker中setClassForTemplateLoading方法参数

代码示例:http://blog.csdn.net/JavisZ/article/details/41810889
你的com.test包下有个Test.java,this.getClass()包名为com.test,而你的ftl文件在com.test.freemarker包(也就是目录)下

Configuration.setClassForTemplateLoading(this.getClass(),"/freemarker");

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

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

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下的模板文件。

猜你喜欢

转载自blog.csdn.net/zuihoudebingwen/article/details/78833363