Freemarker在Maven生成的jar中的哪里加载模板 [英] Where does Freemarker load templates in a maven generated jar

查看:611
本文介绍了Freemarker在Maven生成的jar中的哪里加载模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我澄清一下我目前的做法.我有一个maven项目,其包体系结构如下所示:
src/main/java/com/gearon/app/App.java
src/main/java/com/gearon/app/configuration/Config.java
src/main/java/com/gearon/app/datamodel/*.java

Let me clarify my current practice. I have a maven project and the package architecture looks like below:
src/main/java/com/gearon/app/App.java
src/main/java/com/gearon/app/configuration/Config.java
src/main/java/com/gearon/app/datamodel/*.java

我尝试使用以下代码设置在Config.java中加载模板的目录:

I tried to set directory where to load templates in Config.java with code below:

    cfg = new Configuration();
    cfg.setClassForTemplateLoading(Config.class, "/templates");
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

我已将模板放在 src/main/java/com/gearon/app/templates
架构为 src/main/java/com/gearon/app/templates/*.ftl

I have put templates under src/main/java/com/gearon/app/templates
The architecture is src/main/java/com/gearon/app/templates/*.ftl

如果我直接使用main方法运行代码,则效果很好.但是,当我将项目打包到jar中时,它无法加载模板,并且错误如下所示:

It works fine if I run the code directly with main method. However, when I package the project into a jar, it fails to load templates and the error looks like below:

java.io.FileNotFoundException: Template "index.ftl" not found.

我想知道将这些模板放在哪里.

I'd like to know where to put those tempaltes.

推荐答案

嗯,基于

void setClassForTemplateLoading(Class cl,String basePackagePath)和 void setClassLoaderForTemplateLoading(ClassLoader classLoader,String basePackagePath):当您要通过以下方式加载模板时使用 Java加载类的相同机制(从类路径, 就像他们过去隐约所说的那样).这很可能是首选 加载生产代码模板的方法,因为它允许您 将所有内容保留在部署jar文件中.第一个参数 决定将使用哪个Java ClassLoader.第二个参数 以/分隔指定包含模板的包 格式.请注意,如果您不以/开头,它将被解释 相对于Class参数的包.

void setClassForTemplateLoading(Class cl, String basePackagePath) and void setClassLoaderForTemplateLoading(ClassLoader classLoader, String basePackagePath): These are for when you want to load templates via the same mechanism with which Java loads classes (from the class-path, as they used to say vaguely). This is very likely be the preferred means of loading templates for production code, as it allows you to keep everything inside the deployment jar files. The first parameter decides which Java ClassLoader will be used. The second parameter specifies the package that contains the templates, in /-separated format. Note that if you don't start it with /, it will be interpreted relatively to the package of the Class parameter.

注意事项很重要:

请注意,如果您不以/开头,它将被解释 相对于Class参数的包.

Note that if you don't start it with /, it will be interpreted relatively to the package of the Class parameter.

更改后
cfg.setClassForTemplateLoading(Config.class, "/templates");

cfg.setClassForTemplateLoading(Config.class, "templates");

After change
cfg.setClassForTemplateLoading(Config.class, "/templates");
to
cfg.setClassForTemplateLoading(Config.class, "templates");

一切正常,就像魅力.

这篇关于Freemarker在Maven生成的jar中的哪里加载模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆