温馨提示×

freemarker加载模板路径怎么设置

小亿
175
2024-01-27 19:43:56
栏目: 编程语言

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

  1. 创建Configuration对象:
import freemarker.template.Configuration;

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

// 或者,设置类路径下的模板文件夹路径
cfg.setClassForTemplateLoading(getClass(), "/templates");
  1. 获取模板文件:
Template template = cfg.getTemplate("templateName.ftl");

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

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

0