Java EE6> Packaging JSF facelets (xhtml) and ManagedBeans as JAR

Is it possible to package JSF facelets and ManagedBeans into a JAR file? So that we can use this code and UI combination in different war/ear projects?

I am not talking about JSF Components!

If yes - can you point me to a tutorial or blog post

I need details about the Jar structure and additional files needed in the Jar?

Thanks Max


Yes, that's definitely possible, assuming that you're using JSF 2.0, part of Java EE 6.

As to the managed beans and other JSF classes like validators, converters, etc, just annotate them with @ManagedBean, @FacesValidator, @FacesConverter, etc and package them in the JAR the usual way. You only need to provide a JSF 2.0 compatible /META-INF/faces-config.xml file in the JAR.

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

This way JSF will be triggered to scan the classes in the JAR for JSF specific annotations. Alternatively you can also just register them in the JAR's faces-config.xml the JSF 1.x way.

As to Facelets resources, just drop them in /META-INF/resources folder of the JAR. It'll be treated the same way as public webcontent of the WAR.

See also:

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