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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Arrays of objects as well as arrays of primitive types (for example, int) are also supported. For<br />

arrays of primitive types, the current item for the iteration is automatically wrapped with its<br />

standard wrapper class (for example, Integer for int, Float for float, and so on).<br />

Implementations of java.util.Iterator and java.util.Enumeration are supported, but<br />

they must be used with caution. Iterator and Enumeration objects can't be reset, so they<br />

should not be used within more than one iteration tag. Finally, java.lang.String objects can<br />

be iterated over if the string contains a list of comma-separated values (for example:<br />

Monday,Tuesday,Wednesday,Thursday,Friday).<br />

Here’s the shopping cart iteration from the preceding section, now with the forEach tag:<br />

<br />

...<br />

<br />

<br />

${item.quantity}<br />

<br />

...<br />

<br />

<strong>The</strong> forTokens tag is used to iterate over a collection of tokens separated by a delimiter.<br />

Similarly to the value attribute of the c:set tag (see “Variable Support Tags” on page 203), the<br />

items attribute of forEach and forTokens can also take a deferred value expression so that<br />

<strong>Java</strong>Server Faces tags can be included within these tags.<br />

As described in “Variable Support Tags” on page 203, <strong>Java</strong>Server Faces technology (see<br />

Chapter 10, “<strong>Java</strong>Server Faces Technology”) supports a multiphase life cycle. <strong>The</strong>refore, any<br />

<strong>Java</strong>Server Faces component tags that are included in the forEach tag or the forTokens tag<br />

must have access to the variable referenced by the items attribute at different phases of the life<br />

cycle, not just during the rendering phase. Consider the following code:<br />

<br />

...<br />

<br />

...<br />

<br />

CoreTag Library<br />

<strong>The</strong> items attribute uses a deferred value expression, which means that the book variable it<br />

references is available not only during the rendering phase of the <strong>Java</strong>Server Faces life cycle but<br />

also during the later stages of the life cycle. <strong>The</strong>refore, whatever values the user enters into the<br />

quantity component tags are updated to the external data object during the appropriate stage<br />

of the life cycle.<br />

If the expression referenced by the items attribute used immediate evaluation syntax then the<br />

book variable would be available only when the component is rendered during the render<br />

response phase. This would prevent the values the user enters into the components from being<br />

Chapter 7 • <strong>Java</strong>Server Pages StandardTag Library 207

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

Saved successfully!

Ooh no, something went wrong!