10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Writing Bean Properties<br />

384<br />

Properties for SelectItems Composed of SelectItem Instances<br />

“Using the selectItems Tag” on page 345 describes how the newsletters list of the Duke’s<br />

Bookstore application is populated using the application configuration resource file. You can<br />

also populate the SelectItems with SelectItem instances programmatically in the backing<br />

bean. This section explains how to do this.<br />

In your backing bean, you create a list that is bound to the SelectItem component. <strong>The</strong>n you<br />

define a set of SelectItem objects, set their values, and populate the list with the SelectItem<br />

objects. Here is an example code snippet that shows how to create a SelectItems property:<br />

import javax.faces.component.SelectItem;<br />

...<br />

protected ArrayList options = null;<br />

protected SelectItem newsletter0 =<br />

new SelectItem("200", "Duke’s Quarterly", "");<br />

...<br />

//in constructor, populate the list<br />

options.add(newsletter0);<br />

options.add(newsletter1);<br />

options.add(newsletter2);<br />

...<br />

public SelectItem getNewsletter0(){<br />

return newsletter0;<br />

}<br />

void setNewsletter0(SelectItem firstNL) {<br />

newsletter0 = firstNL;<br />

}<br />

// Other SelectItem properties<br />

public Collection[] getOptions(){<br />

return options;<br />

}<br />

public void setOptions(Collection[] options){<br />

this.options = new ArrayList(options);<br />

}<br />

<strong>The</strong> code first initializes options as a list. Each newsletter property is defined with values. <strong>The</strong>n,<br />

each newsletter SelectItem is added to the list. Finally, the code includes the obligatory<br />

setOptions and getOptions accessor methods.<br />

Properties for SelectItems Composed of SelectItemGroup Instances<br />

<strong>The</strong> preceding section explains how to write the bean property for a SelectItems component<br />

composed of SelectItem instances. This section explains how to change the example property<br />

from the preceding section so that the SelectItems is composed of SelectItemGroup<br />

instances.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!