从类路径设置freemarker模板 [英] Setting freemarker template from classpath

查看:461
本文介绍了从类路径设置freemarker模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,我需要手动获取Freemarker模板-该模板是通过库项目中的类获得的,但是实际的tpl文件包含在该Web应用程序的类路径中.因此,有2个项目,一个为"taac-backend-api",另一个为"taac-web"; taac-backend-api具有获取模板并对其进行处理的代码,但是taac-web是模板的存储位置(特别是在:WEB-INF/classes/email/vendor.tpl中)-我已经尝试过使用将类路径资源扩展为使用Freemarkers setClassForTemplateLoading方法.我认为这会起作用:

I have a web application that I need to manually obtain a Freemarker template - the template is obtained via a class in a library project, but the actual tpl file is contained in the web application classpath. So, there are 2 projects, one 'taac-backend-api' and another 'taac-web'; taac-backend-api has the code to grab the template, and process it, but taac-web is where the template is stores (specifically in: WEB-INF/classes/email/vendor.tpl) - I have tried everything from using springs classpath resource to using Freemarkers setClassForTemplateLoading method. I assume this would work:

    freemarkerConfiguration = new Configuration();
    freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), "");
    Template freemarkerTemplate = freemarkerConfiguration.getTemplate("/email/vendor.tpl");

但是,我总是收到FileNotFoundException.有人可以解释从类路径获取模板的最佳方法吗?

yet, I always get a FileNotFoundException. Can someone explain the best way to obtain a template from the classpath?

谢谢.

推荐答案

这对我最终有用:

freemarkerConfiguration = new Configuration(Configuration.VERSION_2_3_28);
freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), "/");
Template freemarkerTemplate = freemarkerConfiguration.getTemplate("email/vendor.tpl");

这篇关于从类路径设置freemarker模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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