freemarker加載模板路徑怎么設(shè)置

小億
407
2024-01-27 19:43:56
欄目: 編程語言

在Freemarker中,可以通過配置freemarker.template.Configuration對(duì)象來設(shè)置模板路徑。以下是一種常見的設(shè)置方式:

  1. 創(chuàng)建Configuration對(duì)象:
import freemarker.template.Configuration;

Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
  1. 設(shè)置模板路徑:
// 設(shè)置模板文件夾路徑
cfg.setDirectoryForTemplateLoading(new File("path/to/templates"));

// 或者,設(shè)置類路徑下的模板文件夾路徑
cfg.setClassForTemplateLoading(getClass(), "/templates");
  1. 獲取模板文件:
Template template = cfg.getTemplate("templateName.ftl");

在上述代碼中,path/to/templates是模板文件夾的絕對(duì)路徑,而/templates是在類路徑下的相對(duì)路徑。

注意:在設(shè)置模板路徑時(shí)需要注意模板文件的位置和訪問權(quán)限。

0