Packaging Facelets Templates in JAR Files

Posted by & filed under Uncategorized.

In Facelets, the template attribute of the composition tag understands URI references including the jar protocol.  For instance, say I have a layout template called jlayout.xhtml inside a jar file called facelets-t.jar.  The reference can be absolute:
template="jar:file:///C:/j2ee/home/applications/myapp/WEB-INF/lib/facelets-t.jar!/jlayout.xhtml"
Or, relative to the working directory where the JVM was invoked.  In the case of a j2ee server, its probably the j2ee home directory:
template="jar:file:applications/myapp/WEB-INF/lib/facelets-t.jar!/jlayout.xhtml"> Better yet, the template attribute allows an expression.  The base path can reside in a properties file for instance.  Relative references within templates in the jar are relative to the templates path.   So, any references in jlayout.xhtml would be relative to jlayout.xhtml

Now we can package common templates along with our custom Facelets tags and other resources in a jar file that can be reused throughout different projects without having to clone those templates into those projects.  If changes need to be made, dependent projects update the one jar.   This feature makes Facelets a powerful templating technology that can be leveraged over and over again.