Loading FTL templates from JAR in maven

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading FTL templates from JAR in maven

Vijay
Hi,

I am a newbie to FTL and requires your help.

We are building a site which has various modules with war's and jar's and trying to use FTL for front end rendering.

The FTL templates are available as JAR(with different folders inside) in repository and I have to include it as dependency in one of my pom.xml

The ControllerServlet(which extends FreemarkerServlet) is available in one of my JAR module and JSP's are in WAR file

1) Do I need to include the template dependency in JAR or in WAR
2) I have included the dependency in my JAR module and tried to read the templates by mentioning
config.setClassForTemplateLoading(getClass(), "/") in my controller. But I am not able to read the templates.
Can you please guide me on configuration to load the template in ControllerServlet.

If I need to include the dependency in WAR, how do I use FTL to render.
Please guide me.
Reply | Threaded
Open this post in threaded view
|

Re: Loading FTL templates from JAR in maven

Daniel Dekany
The defining class loader of the class that you are using as the first
parameter of setClassForTemplateLoading must be able to access the
resources. That is, ClassLoader.getResource(templateResourcePath) must
work with it. Does it? If it does, then maybe the second parameter to
setClassForTemplateLoading is incorrect ("/" is a strange choice), or
the template names you are using aren't fit to it.

--
Thanks,
 Daniel Dekany


Tuesday, December 24, 2013, 11:44:43 PM, Vijay wrote:

> Hi,
>
> I am a newbie to FTL and requires your help.
>
> We are building a site which has various modules with war's and jar's and
> trying to use FTL for front end rendering.
>
> The FTL templates are available as JAR(with different folders inside) in
> repository and I have to include it as dependency in one of my pom.xml
>
> The ControllerServlet(which extends FreemarkerServlet) is available in one
> of my JAR module and JSP's are in WAR file
>
> 1) Do I need to include the template dependency in JAR or in WAR
> 2) I have included the dependency in my JAR module and tried to read the
> templates by mentioning
> config.setClassForTemplateLoading(getClass(), "/") in my controller. But I
> am not able to read the templates.
> Can you please guide me on configuration to load the template in
> ControllerServlet.
>
> If I need to include the dependency in WAR, how do I use FTL to render.
> Please guide me.
>
>
>
>
> --
> View this message in context:
> http://freemarker.624813.n4.nabble.com/Loading-FTL-templates-from-JAR-in-maven-tp4654896.html
> Sent from the freemarker-devel mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Rapidly troubleshoot problems before they affect your business. Most IT
> organizations don't have a clear picture of how application performance
> affects their revenue. With AppDynamics, you get 100% visibility into your
> Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
> http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
> _______________________________________________
> FreeMarker-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-devel
>


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel
Reply | Threaded
Open this post in threaded view
|

Re: Loading FTL templates from JAR in maven

Vijay
Hi Daniel,

Th eproblem I have is the controller class which I am using is in a package(com.test.freemarker) and the template jar file is loaded from repository by defining in pom.xml. So the template JAR file is available under maven dependencies in my STS.
After complete application build the controller class file is in different jar and template files are in different jar.

PFB the code snippet.

Configuration Class in  com.test.freemarker package.

protected Configuration createConfiguration() {

    config = super.createConfiguration();
    config.setClassForTemplateLoading(getClass(), "/");
   
    return config;
}

There are multiple folders in template JAR in which I need to traverse to get the template file.

Code snippet for reading template file:

try{

Template template = getConfiguration().getTemplate("/<template jar name>/WEB-INF/freemarker/sample.ftl");

} catch(Exception e) {

}
In this case I am getting file not found.
Also I tried loading using various combinations by not providing the jar name etc.,.
But nothing seems to work.
Can you please help me in resolving this.
Reply | Threaded
Open this post in threaded view
|

Re: Loading FTL templates from JAR in maven

Daniel Dekany
Again, check if you can access the resource purely with "core" Java.
That is, with ClassLoader.getResource. If you can't, FreeMarker can't
either.

The jar name surely shouldn't occur in the template name, as that's
not how Java's class loading mechanism works. FreeMarker just uses
that.

--
Thanks,
 Daniel Dekany


Wednesday, December 25, 2013, 4:26:02 PM, Vijay wrote:

> Hi Daniel,
>
> Th eproblem I have is the controller class which I am using is in a
> package(com.test.freemarker) and the template jar file is loaded from
> repository by defining in pom.xml. So the template JAR file is available
> under maven dependencies in my STS.
> After complete application build the controller class file is in different
> jar and template files are in different jar.
>
> PFB the code snippet.
>
> Configuration Class in  com.test.freemarker package.
>
> protected Configuration createConfiguration() {
>
>     config = super.createConfiguration();
>     config.setClassForTemplateLoading(getClass(), "/");
>    
>     return config;
> }
>
> There are multiple folders in template JAR in which I need to traverse to
> get the template file.
>
> Code snippet for reading template file:
>
> try{
>
> Template template = getConfiguration().getTemplate("/<template jar
name>>/WEB-INF/freemarker/sample.ftl");

>
> } catch(Exception e) {
>
> }
> In this case I am getting file not found.
> Also I tried loading using various combinations by not providing the jar
> name etc.,.
> But nothing seems to work.
> Can you please help me in resolving this.
>
>
>
> --
> View this message in context:
> http://freemarker.624813.n4.nabble.com/Loading-FTL-templates-from-JAR-in-maven-tp4654896p4654904.html
> Sent from the freemarker-devel mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Rapidly troubleshoot problems before they affect your business. Most IT
> organizations don't have a clear picture of how application performance
> affects their revenue. With AppDynamics, you get 100% visibility into your
> Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
> http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
> _______________________________________________
> FreeMarker-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-devel
>


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel