How to set the template path for freemarker loading?
In Freemarker, you can set the template path by configuring the freemarker.template.Configuration object. Here is a common way to do this:
- Setting up the system.
import freemarker.template.Configuration;
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
- Set the template path:
// 设置模板文件夹路径
cfg.setDirectoryForTemplateLoading(new File("path/to/templates"));
// 或者,设置类路径下的模板文件夹路径
cfg.setClassForTemplateLoading(getClass(), "/templates");
- Fetch template files:
Template template = cfg.getTemplate("templateName.ftl");
In the above code, “path/to/templates” refers to the absolute path of the templates folder, while “/templates” refers to the relative path within the classpath.
Please be aware of the location and access permissions of template files when setting the template path.