You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by "刘宏飞 (Jira)" <ji...@apache.org> on 2021/01/14 05:22:00 UTC

[jira] [Created] (FREEMARKER-170) Configuration.getTemplate方法应该提供按模板文件流生成Template对象

刘宏飞 created FREEMARKER-170:
------------------------------

             Summary: Configuration.getTemplate方法应该提供按模板文件流生成Template对象
                 Key: FREEMARKER-170
                 URL: https://issues.apache.org/jira/browse/FREEMARKER-170
             Project: Apache Freemarker
          Issue Type: Improvement
          Components: engine
    Affects Versions: 2.3.30
         Environment: java version "1.8.0_171"
IDEA
windows 10
            Reporter: 刘宏飞


在springboot中,我将模板文件test.ftl放在src/main/resources/templates/ 

调用Freemarker API操作模板,主要步骤如下:

Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);

File templateConfigDir = new File(this.getClass().getResource("/templates").getFile());

cfg.setDirectoryForTemplateLoading(templateConfigDir);

Template temp = cfg.getTemplate("test.ftl");

此时模板文件的路径是src/main/resources/templates/ ,在IDEA中测试没有问题。

但是当我把springboot工程打成jar包测试时,会得到一个异常:java.io.FileNotFoundException: file:\C:\Users\Administrator\Desktop\ts\app-1.0.0-SNAPSHOT.jar!\BOOT-INF\classes!\templates does not exist.

 

查看原因,这个目录是jar包中的一个目录,此时无法访问到这个目录。

我的处理方式是:运行代码时,要将模板文件通过流的方式拷贝到工程外的一个目录,然后设置Freemarker:

cfg.setDirectoryForTemplateLoading("工程外的配置目录");

 

希望Freemarker可以提供一个重载API,不需要设置setDirectoryForTemplateLoading,生成Template对象时直接传入模板流(InputStream)。



--
This message was sent by Atlassian Jira
(v8.3.4#803005)