Setting freemarker template from classpath
Asked Answered
P

5

36

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.

Paletot answered 11/6, 2010 at 0:10 Comment(0)
P
86

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");
Paletot answered 11/6, 2010 at 19:46 Comment(1)
Please where suppose "email/vendor.tpl" to be ?Millenarianism
D
11

In 2017, the following is deprecated:

Configuration conf = new Configuration();

We should pass freemarker.template.Version to the constructor:

Configuration conf = new Configuration(new Version(2, 3, 23));
conf.setClassForTemplateLoading(Application.class, "/views");

where the version numbers refer to the current version of FreeMarker.

The views directory is located in src/main/resources.

Demerit answered 5/3, 2017 at 17:56 Comment(0)
K
5
freemarkerConfiguration = new Configuration();
freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), "");
Template freemarkerTemplate = freemarkerConfiguration.getTemplate("template.tpl");

Use this method to load the classes from the package where your class is located, so if your class is

org.foo.SomeClass the templates will be looked for in /org/foo in the classpath. This keeps your templates stored with the class that uses/loads them.

Kreager answered 16/10, 2013 at 15:35 Comment(2)
But if I want to keep the template in under resoources/template folder, how can i do that?Whitneywhitson
@Whitneywhitson found any solutions for this?Ickes
R
1

If you are using Struts 2 and the Conventions plugin, wuntee's solution doesn't seem to work: setClassForTemplateLoading in turn creates an instance of ClassTemplateLoader which doesn't find files in jars no matter what path prefix is specified.

Instead, create an instance of StrutsClassTemplateLoader. (I do this in a custom sub-class of FreemarkerManager in its getTemplateLoader method.) It takes no parameters, so presumably it just knows how Struts and Conventions do things.

Roid answered 27/5, 2011 at 19:3 Comment(1)
Depending on the class loader hierarchy, sometimes it has to be considered carefully what class you specify for ClassTemplateLoader. Often the best is to use the ClassTemplateLoader constructor that takes a ClassLoader directly (instead of a Class - since 2.3.22), and then passing in the thread context class loader of the web application.Toms
G
0

Use the following config and place it in application properties.

spring.freemarker.template-loader-path=
Gaven answered 24/5, 2020 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.