[ 
https://issues.apache.org/jira/browse/FREEMARKER-170?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17264667#comment-17264667
 ] 

Dániel Dékány commented on FREEMARKER-170:
------------------------------------------

First of all, I'm not sure if you realize that you should use 
{{cfg.setClassForTemplateLoading}} in this case. {{src/resources}} is not 
really files in Java, but "resources".

{{Class.getResource(...).getFile()}} isn't always possible, as classes can be 
loaded from any kind of source. Even in the case you show, the file it returns 
is kind of fake, because you can't really pass that path to the Java file 
API-s. I don't know why they even support {{URI.getFile()}} for this case. Now, 
in principle, I could detect that the path points inside a jar or zip, and then 
use a new {{TemplateLoader}} that can load template from inside zip archives. 
But I'm not sure if I want to do that, as in the typical scenario where this 
comes up, the more correct solution would be using 
{{cfg.setClassForTemplateLoading}}.

Also please use English!


> 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: 刘宏飞
>            Priority: Major
>              Labels: features, patch
>
> 在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)

Reply via email to