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.
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.
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With