HTML and CSS Reference
Using the ResponseWriter on the FacesContext it is fairly simple to output the desired markup. ResponseWriter
contains methods for starting new elements, adding attributes to existing elements, and ending existing elements.
All we need to provide is the name and value of the element or attribute, and which component they belong to.
To create the <div> element we use the startElement method. While the element has not yet been closed you can add
attributes by using the writeAttribute method. To add text inside the element you can use the write or writeText
methods. writeText will escape any HTML in the string, whereas write dumps the given content without escaping.
Lastly, you can use the endElement method to close the currently opened element. You can any number of elements
and nested elements in your output but it is good practice to enclose your component in a single div with the ID
attribute set to the clientId attribute of the component. This makes it easy to locate your component as well as to
update the component using Ajax.
■ You should always use writetext when receiving content from the user unless you trust that the user doesn't
Step 4—Create a Tag Library Descriptor
The purpose of the TLD is to define and expose the component and renderer as a tag to the JSF Framework and to the
A TLD file defines a single namespace with all the tags for the given namespace. The namespace is based on
standard XML namespaces to avoid naming conflicts when you have multiple tag libraries provided by different
developers. An XML namespace is a unique URI that is defined by the vendor of the tag library. In our case we have
selected the namespace http://com.apress.projsf2html5/randomtext . To use the tag library the page author must
declare that he wants to use the namespace and a prefix for calling the library. An example of declaring the use of the
tag library with the rt prefix can be seen in Listing 6-7. You can select any prefix you want for the namespace.
Listing 6-7. Declaring the Use of a Tag Library
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
With the namespace defined each of the tags in the namespace listed. A tag must be declared with the
information listed in Table 6-3 .