Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )
Daniel Dekany <[email protected]> Fri, 22 Feb 2013 01:19:44 +0100
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
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.
--
Best regards,
Daniel Dekany
> then here is how I used it
>
> ClassTemplateLoader ctl = new FixedClassTemplateLoader(getClass(),
> "templates");
> TemplateLoader[] loaders = new TemplateLoader[] { ctl };
> MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
> cfg.setTemplateLoader(mtl);
> Template template =
> cfg.getTemplate("templates/testEmailTemplate.ftl");
>
> ----
>
> Now I am able to load the template and generate my email template.
>
> Thanks
------------------------------------------------------------------------------
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