Packaging Facelets files (templates, includes, composites) in a JAR

Fabio picture Fabio · Aug 20, 2010 · Viewed 15.8k times · Source

Is it possible to put JSF2 Facelets files with common content into a JAR to use it from other web applications inside e.g. <ui:composition template>, <ui:include src>, <cc:implementation>, etc? If yes, how can I achieve this? Is some extra configuration necessary?

Answer

BalusC picture BalusC · Dec 23, 2011

You can put common resources in the /META-INF/resources folder of the JAR which is to be treated like as /WEB-INF/resources folder of the WAR.

E.g.

CommonWebProject
 |-- META-INF
 |    |-- resources
 |    |    `-- common
 |    |         |-- css
 |    |         |    `-- some.css
 |    |         |-- js
 |    |         |    `-- some.js
 |    |         |-- images
 |    |         |    `-- some.png
 |    |         |-- components
 |    |         |    `-- somecomposite.xhtml
 |    |         |-- someinclude.xhtml
 |    |         `-- sometemplate.xhtml
 |    |-- faces-config.xml
 |    `-- MANIFEST.MF
 :

The resources of the JAR are then available as follows:

<... xmlns:common="http://xmlns.jcp.org/jsf/composite/common/components">
<h:outputStylesheet library="common" name="css/some.css" />
<h:outputScript library="common" name="js/some.js" />
<h:graphicImage library="common" name="images/some.png" />
<common:somecomposite />
<ui:include src="/common/someinclude.xhtml" />
<ui:composition template="/common/sometemplate.xhtml" />
...

If you want to trigger the JSF2 annotation scanner as well so that you can put @ManagedBean, @FacesValidator, @FacesConverter and consorts in that project as well, then create a JSF2 compatible /META-INF/faces-config.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
</faces-config>

The Facelets resource resolver is only necessary if the resources are not in /META-INF/resources for some reason, or when you're not using Servlet 3.0 but 2.5, or when you're using an early JBoss/JSF version which has bugs in META-INF resource resolving. See also How to create a modular JSF 2.0 application? for a concrete example.