在Freemarker中,可以通過配置freemarker.template.Configuration
對(duì)象來設(shè)置模板路徑。以下是一種常見的設(shè)置方式:
Configuration
對(duì)象:import freemarker.template.Configuration;
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
// 設(shè)置模板文件夾路徑
cfg.setDirectoryForTemplateLoading(new File("path/to/templates"));
// 或者,設(shè)置類路徑下的模板文件夾路徑
cfg.setClassForTemplateLoading(getClass(), "/templates");
Template template = cfg.getTemplate("templateName.ftl");
在上述代碼中,path/to/templates
是模板文件夾的絕對(duì)路徑,而/templates
是在類路徑下的相對(duì)路徑。
注意:在設(shè)置模板路徑時(shí)需要注意模板文件的位置和訪問權(quán)限。