Jim Driscoll's Blog

Notes on Technology and the Web

Writing a simple Composite Component with JSF 2.0

leave a comment »

One of the pain points for JSF has always been the complexity that you face in creating components. In JSF 2.0, creating a new component that’s made up of existing components is a snap.

Here’s a quick example of how you can create a new component, and use it in your page.

For this example, I wanted to create a simple text box that has a yellow background. Useful? No. Simple? Yes 🙂 In fact, it only needs two files. (This may look like a bit of code for a blog entry, but bear with me – most of it is just xhtml fluff. There’s only about 4 or 5 lines that you’ll actually need to learn.)

The first file is the page itself. Since JSF 2.0 uses Facelets as the preferred way to create pages, we’ll use that. Here’s the page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml"
    <title>Yellow Text Example</title>
        <h1>Output Text Example</h1>
        <h:form id="form1">
                <ez:out value="Test Value"/>
            <p><h:commandButton value="reload"/></p>

Two lines you need to care about here:


defines the composite component library, and
<ez:out value="Test Value"/>
then uses the composite component. But where is the “simpleout” component library? It’s in the directory WEB-INF/resources/simpleout, and the “out” component is defined in a file named out.xhtml in that directory. Here’s the contents of that file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml"
<title>This will not be present in rendered output</title>

    <composite:attribute name="value" required="false"/>

    <h:outputText value="#{cc.attrs.value}" style="background-color: yellow"/>

Again, there’s really two bits to look at here. The first is
<composite:attribute name="value" required="false"/>
says that our out component can take one attribute, named “value”, and that it’s presence is optional. Then,

<h:outputText value="#{cc.attrs.value}" style="background-color: yellow"/>

is where we actually define the component, with the “value” attribute set to the value attribute that’s been passed in.

And that’s it. No Java code is required for this very simple example.

I’ll do other blogs about how to create more complex composite components, but I hope that I’ve made my point about how very simple this new way of component creation can be.

As always, if you have any questions, feel free to ask. And remember that you can try out this example by downloading GlassFish, and updating to JSF EDR2.

Update: I’ve updated this example to be more clear, and to conform with the latest JSF 2.0 PR release.

(This article was originally published on my java.net blog on November 1, 2008.)


Written by jamesgdriscoll

February 9, 2010 at 6:30 PM

Posted in Java, JSF, web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: