我有一个web应用程序,我需要手动获取Freemarker模板 - 该模板是通过库项目中的类获取的,但实际的tpl文件包含在Web应用程序类路径中。所以,有两个项目,一个是'taac-backend-api',另一个是'taac-web'。 taac-backend-api具有获取模板并处理它的代码,但taac-web是模板存储的地方(具体位于:WEB-INF/classes/email/vendor.tpl) - 我尝试了使用将类路径资源弹出以使用Freemarkers setClassForTemplateLoading方法。我认为这将工作:从类路径设置freemarker模板
freemarkerConfiguration = new Configuration();
freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), "");
Template freemarkerTemplate = freemarkerConfiguration.getTemplate("/email/vendor.tpl");
但是,我总是得到一个FileNotFoundException。有人可以解释从类路径获取模板的最佳方式吗?
谢谢。
根据类加载器层次结构,有时必须仔细考虑您为“ClassTemplateLoader”指定的类。通常最好的方法是使用'ClassTemplateLoader'构造函数,该构造函数直接使用'ClassLoader'(而不是'Class' - 自2.3.22开始),然后传入Web应用程序的线程上下文类加载器。 – ddekany 2015-03-14 09:49:53