JSF Tip #45 - Create a composite component with a custom namespace

When you developed a composite component the namespace you would be seeing would look like "http://java.sun.com/jsf/composite/xxxxxx". But what if you are not allowed to use it that way or you just do not like it, is it possible to change that? Yes it is possible. This blog entry shows you how.

The composite component


    <?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:cc="http://java.sun.com/jsf/composite">
        <body>
            <cc:interface></cc:interface>
            <cc:implementation>
                This is coming from a composite component with its own custom namespace! Hurray :)
            </cc:implementation>
        </body>
    </html>
        

The .taglib.xml file


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

    <facelet-taglib 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-facelettaglibrary_2_0.xsd"
                    version="2.0">
        <namespace>http://www.mypanel.com/mypanel</namespace>
        <composite-library-name>mypanel</composite-library-name>
    </facelet-taglib>
        

The web.xml


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

    <web-app version="3.0" 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-app_3_0.xsd">
        <context-param>
            <param-name>javax.faces.PROJECT_STAGE</param-name>
            <param-value>Development</param-value>
        </context-param>
        <context-param>
            <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
            <param-value>/WEB-INF/mypanel.taglib.xml</param-value>
        </context-param>
        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>/faces/*</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>30</session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>faces/index.xhtml</welcome-file>
        </welcome-file-list>
    </web-app>
        

And now lets use it!


    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:mypanel="http://www.mypanel.com/mypanel">
        <h:head>
            <title>Composite component</title>
        </h:head>
        <h:body>
            <h2>Composite component</h2>
            <mypanel:mypanel/>
        </h:body>
    </html>
        

Posted November 22, 2013

Up