Skip to main content

JSF Tip #43 - Package a composite component

Posted by mriem on November 20, 2013 at 12:58 PM PST

If you want to share a composite component between projects you should package it in a JAR.

But how would you do it? Well it is pretty easy. First include the resources in the META-INF/resources directory. E.g.

<?xml version='1.0' encoding='UTF-8' ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cc="http://java.sun.com/jsf/composite">
    <body>
        <cc:interface></cc:interface>
        <cc:implementation>
            This is coming from a JAR-packaged composite component! Hurray :)
        </cc:implementation>
    </body>
</html>

And then add a faces-config.xml in the META-INF directory. E.g.

<?xml version='1.0' encoding='UTF-8'?>

<faces-config version="2.1"
    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_1.xsd">

</faces-config>

And then JAR it all up.

Note the code for this sample is available as part of the Glassfish samples (see the subversion repository at https://svn.java.net/svn/glassfish-samples~svn/trunk/ws/javaee7/jsf/packageCompositeComponent/)

And that is it.

Enjoy!

Related Topics >>