freemarker加载模板路径怎么设置

在Freemarker中,可以通过配置freemarker.template.Configuration对象来设置模板路径。以下是一种常见的设置方式:

创建Configuration对象:

import freemarker.template.Configuration;

Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);

设置模板路径:

// 设置模板文件夹路径
cfg.setDirectoryForTemplateLoading(new File("path/to/templates"));

// 或者,设置类路径下的模板文件夹路径
cfg.setClassForTemplateLoading(getClass(), "/templates");

获取模板文件:

Template template = cfg.getTemplate("templateName.ftl");

在上述代码中,path/to/templates是模板文件夹的绝对路径,而/templates是在类路径下的相对路径。

注意:在设置模板路径时需要注意模板文件的位置和访问权限。

阅读剩余
THE END