Skip to content

Custom taglibs with facelets

Creating tag libraries using facelets might be cool but needs more redundant infrastructure than the new JSP tag libs (which doesn’t need tld declaration or web.xml context params). However once your done, building reusable tags on the fly (with no java code) can be pretty slick.

Here are the steps for creating a simple tag – iconButton, which decorates a button with an icon image and other misc useful attributes.

taglib descriptor: your-taglib.xml (place it in WEB-INF)

<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd">
<facelet-taglib>
    <namespace>http://yourUrlHere</namespace>
    <tag>
       <tag-name>iconButton</tag-name>
       <source>view/tags/iconButton.xhtml</source>
     </tag>
</facelet-taglib>

iconButton.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<button title="#{title}" class="#{styleClass}"
    onclick="#{empty onclick ? '' : onclick}">
    <img src="#{iconSrc}" border="0" align="top"
    style="padding-top:1px;padding-bottom:1px;"/>
   #{text}
</button>
</ui:composition>

Usage (in other facelets)
somepage.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:myTags="http://yourUrlHere"

    <myTags:iconButton title=" Search "
                                styleClass="iconButton" style="vertical-align:top;"
                                iconSrc="./img/magnifying-glass.png"
                                text="Search "/>

Note the inclusion of the namespace (“yoururl”) above.

web.xml

<context-param>
    <param-name>facelets.LIBRARIES</param-name>
    <param-value>/WEB-INF/tomahawk-taglib.xml;/WEB-INF/my-taglib.xml</param-value>
</context-param>

You may get the following error: -

Caused by: java.io.FileNotFoundException: <jboss-home>\bin\facelet-taglib_1_0.dtd

That’s probably because you haven’t declared the taglib in WEB-INF, as shown above.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*