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.

...<br />

<br />

connectionId<br />

false<br />

<br />

<br />

Tag Handler Examples<br />

<strong>The</strong> simple tags described in this section demonstrate solutions to two recurring problems in<br />

developing JSP applications: minimizing the amount of <strong>Java</strong> programming in JSP pages and<br />

ensuring a common look and feel across applications. In doing so, they illustrate many of the<br />

styles of tags discussed in the first part of the chapter.<br />

An IterationTag<br />

Constructing page content that is dependent on dynamically generated data often requires the<br />

use of flow control scripting statements. By moving the flow control logic to tag handlers, flow<br />

control tags reduce the amount of scripting needed in JSP pages. Iteration is a very common<br />

flow control function and is easily handled by a custom tag.<br />

<strong>The</strong> discussion on using tag libraries in Chapter 5, “<strong>Java</strong>Server Pages Technology,” introduced a<br />

tag library containing an iterator tag. <strong>The</strong> tag retrieves objects from a collection stored in a<br />

<strong>Java</strong>Beans component and assigns them to an EL variable. <strong>The</strong> body of the tag retrieves<br />

information from the variable. As long as elements remain in the collection, the iterator tag<br />

causes the body to be reevaluated. <strong>The</strong> tag in this example is simplified to make it easy to<br />

demonstrate how to program a custom tag. web applications requiring such functionality<br />

should use the JSTL forEach tag, which is discussed in “Iterator Tags” on page 206.<br />

JSP Page<br />

<strong>The</strong> index.jsp page invokes the iterator tag to iterate through a collection of department<br />

names. Each item in the collection is assigned to the departmentName variable.<br />

<br />

<br />

<br />

Departments<br />

<br />

<br />

<br />

<br />

<br />

Departments<br />

Programming SimpleTag Handlers<br />

Chapter 8 • CustomTags in JSP Pages 263

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

Saved successfully!

Ooh no, something went wrong!