Skip to main content

JSF 2.0: A Component's Controller

Posted by driscoll on January 31, 2009 at 4:49 PM PST

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:

[prettify]
@ManagedBean(name="multinumber")
@SessionScoped
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;
    }
[/prettify]


As you can see, just two int values.


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

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


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:

                     displayName="Very Basic Output Component"
                     shortDescription="A basic example of the composite component feature">
  
       
       
  




   
   

   
   


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.

Related Topics >>

Comments

Hey guys - To ask general questions about JSF 1.2, JSF 2.0, or any other Java EE topic not covered in my blog, try sending an email to webtier@glassfish.dev.java.net, or post on the webtier forum at http://forums.java.net/jive/forum.jspa?forumID=138 I, along with most of the JSF team, monitor that alias and answer questions there. That way, the answers are seen by a wider audience, and are searchable by people wanting answers months in the future. As always, I'm happy to answer questions that actually relate the topic I'm blogging about.

Good morning. Can you help me Please? I have a problem with JSF with Firefox 3.x, here some components like a JSF table is not displayed well, not refresh completely de page. The errors that to be showed woodstock is not defined.. Do yuo know as resolve this kind the troble...? Best regard Jim.

Oh, I figured out than one cannot use h:column inside my:column, because the structure will be h:dataTable / my:column / h:column, so h:dataTable will ignore my:column

Hi, Just wanted to find out if JSF2 will have better support for Collections other than java.util.List. For some of us who make use of java.util.Set implementations, JSF has been very hard to deal with.

Well, since I mentioned it. My problem could be reproduced if you try to put a SwitchList component into some outer component with renderUsingPageChildren, then buttons work fine, but selectItems do not. Especially, if you change switchlist to use a controller, but I suspect that is not related.

I wonder, if it is possible to create something like custom h:datatable. Say to define my:datatable (which contains ) and my:column ? Especially so that one would be able to use attributes passed to my:datatable in my:column? Are there any sideeffects when a component is used it another (possibly included via renderUsingPageChildren? For instance if there's an ui:repeat in the outer component is it's variable visible in the inner? (Actually, I tried implementing this with my:datatable using h:dataTable with "row" iteration variable, hoping user could reference it in my:column, but I stopped at some earlier stage when I found that the parameter passing works badly if the component is wrapped by another simple component with renderUsingPageChildren (used to be able to show/hide inner content).