Jar包内加载FreeMarker模版

Questions

Loading freemarker templates folder inside .jar

Solution

Configuration cfg;
private Template template;  
    {
        cfg=new Configuration(); 
        try {
            cfg.setClassForTemplateLoading(this.getClass(), "/templates");
            template = cfg.getTemplate("template.ftl");
}

others

i have my templates in src/main/resources/templates folder but, the java code is in src/main/java/com/app package. In this case, how should i refer to the templates folder ? – yathirigan Jun 29 '15 at 13:36
You could move your templates to src/main/resources/com/app/templates or change from this.getClass() to XXX.class, where XXX is a class right under src/main/java – Werner Kvalem Vesterås Jun 29 '15 at 13:41
实际上不需要移动到那个目录下面也可以生效,关键是在跟路径即可。

Reference

[1]. java工程中使用freemarker例子
[2]. Loading freemarker templates folder inside .jar
[3]. JAVA获取CLASSPATH路径与FreeMarker设置模板路径
[4]. Freemarker中Configuration的setClassForTemplateLoading方法参数问题
[5]. freemarker加载模板文件的三种方法
[6]. freemarker源码解读之一–概述
[7]. java代码中获取classpath路径

相关文章
相关标签/搜索