Jim Driscoll's Blog

Notes on Technology and the Web

JSF 2.0: A Component’s Controller

leave a comment »

After my last blog post on composite components, Ed Burns, the spec lead, pointed out a better way to do things, by using a controller for the component. Let’s go over a simple example of how that would work. The example won’t actually do anything (unlike most of my examples, which at least pretend to perform a function) – I just want to sketch out a single, simple concept, and show it in code.

We’ll create a component that takes a single argument – but that argument will actually consist of two different values, bundled into a single bean. Here’s the meat of the bean’s code:

public class MultiNumberHolder {

    private Integer i1 = 1;
    private Integer i2 = 2;

    public void setNumber1(Integer i) {
        this.i1 = i;

    public Integer getNumber1() {
        return i1;

    public void setNumber2(Integer i) {
        this.i2 = i;

    public Integer getNumber2() {
        return i2;

As you can see, just two int values.

And to see how you’d use this in a page, here’s the code:

<ez:double controller="#{multinumber}"/>

Where, of course, you’ve defined ez as a composite component library, and double is a component in that library. So, let’s look at the double.xhtml library’s interface and implementation sections:

<cc:interface name="double"
                     displayName="Very Basic Output Component"
                     shortDescription="A basic example of the composite component feature">
   <cc:attribute name="controller" required="true">
        <cc:attribute name="number1" required="true"/>
        <cc:attribute name="number2" required="true"/>

    <h:inputText value="#{cc.attrs.controller.number1}"/>
    <h:inputText value="#{cc.attrs.controller.number2}"/>

I'm pretty sure that this doesn't need much explanation, but just in case - I've declared that the component takes one attribute - controller, and that that controller has two fields, number1 and number2. Then, in the implementation section, I can reference those fields with a simple dot notation.

As always, this code is available in Mojarra's jsf-demo directory, in this case, under the basic-ezcomp directory.

(This article originally published on my java.net blog on Febuary 1, 2009.)


Written by jamesgdriscoll

February 9, 2010 at 8:43 PM

Posted in JSF

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: