Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting freemarker template from classpath

Tags:

freemarker

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"); 

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

Thanks.

like image 479
wuntee Avatar asked Jun 11 '10 00:06

wuntee


People also ask

How do I set up FreeMarker?

First you have to create a freemarker. template. Configuration instance and adjust its settings. A Configuration instance is the central place to store the application level settings of FreeMarker.

How do I use FreeMarker template in spring boot?

Freemarker Template FilesSpring boot looks for the The template files under classpath:/templates/. And for the file extension, you should name the files with . ftlh suffix since Spring Boot 2.2. If you plan on changing the location or the file extension, you should use the following configurations.

What is FreeMarker template?

Apache FreeMarker is a free Java-based template engine, originally focusing on dynamic web page generation with MVC software architecture. However, it is a general purpose template engine, with no dependency on servlets or HTTP or HTML, and is thus often used for generating source code, configuration files or e-mails.


1 Answers

this is what ended up working for me:

freemarkerConfiguration = new Configuration(Configuration.VERSION_2_3_28); freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), "/"); Template freemarkerTemplate = freemarkerConfiguration.getTemplate("email/vendor.tpl"); 
like image 99
wuntee Avatar answered Sep 22 '22 12:09

wuntee