Reuse facelets xhtml files and taglibs from jar archives

I have asked myself, if it is possible to use facelets xhtml files and taglibs from a jar file instead from the whole web application.

The short anwser: Yes, it is possible ;-)

The facelets documentation gives a hint how to use the tag lib from a jar file. But you cannot found in the documentation, if it is possible also to use a xhtml file (referenced from the taglib for example). Of course facelets uses the same approach to load xhtml files from a jar file as for the tag library.

Example:
The follow tag lib definition is placed in my JAR project under /META-INF/myProject.taglib.xml


<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">

<facelet-taglib>

    <namespace>http://thomaswabner.wordpress.com/waffel</namespace>

    <tag>
        <tag-name>select</tag-name>
        <source>components/select.xhtml</source>
    </tag>

</facelet-taglib>

The select.xhtml file is placed in the same project under /META-INF/components/select.xhml and contains some facelets definitions (ui component with a selectbox fo example). The point is, that facelets searches the jar archive under the /META-INF directory for the xhtml file.

Now you can simple use the tag in you web application. You only need to include the jar file with the tag library and xhtml file in you web application classpath.


<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:waffel="http://thomaswabner.wordpress.com/waffel"
          version="2.0">
  <ui:component>
     <waffel:select/>
  </ui:component>
</jsp:root>

Update: See comments. I have created a small example.

5 Antworten

  1. Hi Thomas,

    I try to reuse facelets templates from an jar file, too. I didn’t manage to this. Would It be possible to publish an example.jar?

    Thanks in advance,

    Achim.

  2. I have created the example. It contanis an jar file (facelet-embedded) and a war file (facelet-example). The war file simple uses the jar file.

    It runs in tomcat6 without problem.

    You can download the file here:

    https://hora-obscura.de/dav_waffel/facelet-embedded.zip
    https://hora-obscura.de/dav_waffel/facelet-example.zip

    I have created the projects with maven and the myfaces facelets arachetype. But the approach works also with SUN RI and facelets.

  3. Hi Thomas,
    do you know how a JavaScript-File (or a image, etc.), which is also included in the jar, must be referenced in the *.xhtml to work.

    Thanks in advance,
    Kai

  4. Hi Kai,

    currently I have not testet, how to include other resources in the JAR. If I have some time, I will have a look.

    Regards,

    - waffel

  5. [...] Reuse facelets xhtml files and taglibs from jar archives « waffel’s Weblog (tags: jsf facelets) [...]

Einen Kommentar schreiben