Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

akuntamukkala <[email protected]> Thu, 21 Feb 2013 22:36:44 -0800 (PST)
Newsgroups gmane.comp.web.freemarker.user
Message-ID <CD4C6D5F.3957%[email protected]>
I think the issue stems from the difference between class.getResouce() and
classLoader.getResource() which is why the file wasn't found when I used the
former. 


*****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]"
<[email protected]>
Date:  Thursday, February 21, 2013 6:20 PM
To:  Ashwini Kuntamukkala <[email protected]>
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.

-- 
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
_______________________________________________
FreeMarker-user mailing list
[hidden email] </user/SendEmail.jtp?type=node&node=4654454&i=0>
https://lists.sourceforge.net/lists/listinfo/freemarker-user



If you reply to this email, your message will be added to the discussion
below:
http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-Clas
sTemplateLoader-problem-or-dev-hands-tp625098p4654454.html
To unsubscribe from Loading templates from classpath ( ClassTemplateLoader
problem or /dev/hands ? ), click here
<http://freemarker.624813.n4.nabble.com/template/NamlServlet.jtp?macro=unsub
scribe_by_code&node=625098&code=YXNod2luLmt1bnRhbXVra2FsYUBzY2lzcGlrZS5jb218
NjI1MDk4fC0xMTYyMDA0NzAz> .
NAML 
<http://freemarker.624813.n4.nabble.com/template/NamlServlet.jtp?macro=macro
_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.Ba
sicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.templa
te.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instan
t_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>






--
View this message in context: http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-ClassTemplateLoader-problem-or-dev-hands-tp625098p4654456.html
Sent from the freemarker-user mailing list archive at Nabble.com.

------------------------------------------------------------------------------
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

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user