Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )
Daniel Dekany <[email protected]> Sat, 23 Feb 2013 10:12:11 +0100
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Since people use ClassTemplateLoader for ages to load templates from jar-s,
I suspect the problem is elsewhere. Looking at your example:
ClassTemplateLoader ctl = new FixedClassTemplateLoader(getClass(), "templates");
...
Template template = cfg.getTemplate("templates/testEmailTemplate.ftl");
In the first line you establish that the template root directory is
"templates", yet in the last line you write
"templates/testEmailTemplate.ftl", not "testEmailTemplate.ftl". With
ClassTemplateLoader that would mean that the final classpath-resource
path is "templates/templates/testEmailTemplate.ftl". But your template
loader ignores the base path, so I suppose, that's why it works. What
you had to do is either using cfg.getTemplate("testEmailTemplate.ftl")
or using new ClassTemplateLoader(getClass(), ""). Is possible that
this is the solution?
--
Best regards,
Daniel Dekany
Friday, February 22, 2013, 6:47:51 AM, akuntamukkala wrote:
> Daniel
> Thanks for taking time to respond.
>
> The problem I was having is that the
> "templates/testEmailTemplate.ftl" which is in a jar file wasn't
> getting loaded when I used ClassTemplateLoader.
>
> log4j:WARN No appenders could be found for logger (freemarker.cache).
> log4j:WARN Please initialize the log4j system properly.
> Exception in thread "main" java.io.FileNotFoundException: Template testEmailTemplate.ftl not found.
> at
> freemarker.template.Configuration.getTemplate(Configuration.java:489)
> at
> freemarker.template.Configuration.getTemplate(Configuration.java:452)
>
> Upon digging up ClassTemplateLoader {free marker v2.3.9} source, I
> found getUrl() to be defined as follows.
> protected URL getURL(String name)
> {
> return loaderClass.getResource(path + name);
> }
>
> It seems valid. However this wasn't loading the .ftl file from
> templates folder. I explicitly initialized ClassLoader as shown the
> code in the previous email and it worked.
>
> I understand the intent of this class but wasn't able to make it
> work for me so I used that class loader to load the resource.
>
>
> Thank you,
> Ashwin
>
>
>
>
> *****This e-mail and replies and forwards are for the sole use of
> the above individual(s) or entities and may contain proprietary,
> privileged and/or highly confidential information. Unauthorized use is strictly prohibited.*****
>
>
> From: "Daniel Dekany [via FreeMarker]" <[hidden email]>
> Date: Thursday, February 21, 2013 6:20 PM
> To: Ashwini Kuntamukkala <[hidden email]>
> Subject: Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )
>
> Thursday, February 21, 2013, 7:32:01 PM, akuntamukkala wrote:
>
>> Here is what I did to get around this problem with ClassTemplateLoader
>>
>> The issue is that the proper class loader isn't being used. I got around
>> this by subclassing ClassTemplateLoader
>>
>> public class FixedClassTemplateLoader extends ClassTemplateLoader {
>>
>> private ClassLoader classLoader;
>> public FixedClassTemplateLoader(Class clazz, String string) {
>> super(clazz, string);
>> classLoader = clazz.getClassLoader();
>> }
>> @Override
>> protected URL getURL(String name) {
>> // this now loads the template file from the jar
>> return classLoader.getResource(name);
>> }
>> }
> So, what exactly does this fix and how? As far as I see,
> ClassTemplateLoader already uses the defining class-loader of the
> `clazz` constructor argument, so there's no change here. Or is there?
>
> BTW, the other change is that you ignore the 2nd constructor parameter
> (the base path) in the getURL method for some reason, although if you
> really don't want a base path, you could just use "" for it as the
> constructor argument.
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb