advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement



Title: JSP Tag Libraries
ISBN: 193011009X
US Price: $35.96
Publication Date: May 2001
Pages: 656
© 2001 Manning Publications Co.

Reviews : Java Books :
JSP Tag Libraries : Chapter 10: Iterating with Tags

Iterating with tags

In this chapter

  • Iterating with tags 101
  • Universal iteration with tags (iterate anything!)
  • Tag-only presentation of a shopping cart
  • The JSP1.2 IterationTag Iterating With Tags

At the end of chapter 8, we used our newly created JavaBean tags to export an Enumeration which was then iterated over with a scriptlet. Let's take another look at this JSP fragment.

	
<table> 
<tr> 
<th> Header </ th> <th> Value </th> 
</tr> 

<bean: export id="e" type=" enum" object="<%= request %>" property="headerNames" /> <% while(e.hasMoreElements()) { String name = (String)e.nextElement(); %> <tr> <td> <%= name %> </td> <td> <bean: show object="<%= request %>" property=" header" index="<%= name %>"/> </td> </tr> <% } %> </table>

As you can see (note the highlighted code), although our JavaBean tags greatly reduce the need for scriptlets, we are still unable to avoid them when working with indexed JavaBean properties that have more than one value. In cases of multivalued properties (Enumerations, arrays, etc.) we typically want to loop through (iterate) and perform a function with each value in the property. Without a tag to handle this iteration, we're left using a scriptlet like the one here. This is unfortunate since we want to be able to provide our JSP authors with the ability to perform common functions on JavaBeans without prior knowledge of Java. Ideally, we'd like to offer them a very user-friendly JSP custom tag that would work for iteration.

Iteration, especially enumerating some value, can be very declarative, and, as we've seen, declarative tasks are easily performed with tags. For example, by using iteration tags we can modify the previous JSP fragment:

<table> 
<tr> 
<th> Header </th> <th> Value </th> 
</tr> 
<iter:foreach id="name" 
type="String" 
object="<%= request %>" 
property="headerNames" />

<tr> 

<td> <%= name %> </td> 

<td> 
	<bean: show object="<%= request %>" 
		property=" header" 
		index="<%= name %>"/>
</td> 
</tr> 
<iter:foreach>
</table> 

This is obviously quite an improvement.

Why should we bother creating special iteration tags when a two-line scriptlet hardly seems demanding for a Java developer? Again, we can't forget that the goal of building custom tag libraries is to make it possible for non-Java developers (presentation/ HTML developers) to build complex sites. Though iteration using scriptlets may not be complex for the Java programmer, it does require the JSP developer to:

  • Know how to iterate on different Java types— Enumerations, Iterators, arrays, and so forth. To further complicate the situation, iteration methods usually return an Object that the JSP developer will have to cast. .
  • Position the curly brackets in the correct location. If the JSP developer forgets a curly bracket, the JSP compilation will fail, usually with a relatively obscure error message. .
  • Maintain and debug yet another portion of Java code.

As a result, iteration tags are necessary to enhance the effectiveness of our JavaBean tags and to keep our JSPs scriptlet-free.

This chapter explores iteration with tags and shows how to build JSP custom tags that perform iteration for us. We'll start with a brief introduction to iterating with custom JSP tags and discuss their design principles; later, we will develop iteration tags to handle cases in which we wish to iterate over Java's common object containers.

NOTE In this chapter, you will see the word iterator used in two distinct ways. When we use the generic term, we mean any multivalued object (be it an Array, an implementation of the java.util.Enumeration interface or

an implementation of the java.util.Iterator interface). When we mention Iterator we are speaking strictly about the Java interface.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 38
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity
Microsoft's Novell Investment Tops $340M
Fedora 10 Takes Shape
IBM Gives a Mobile Voice to Developers
Inadequate Tools Send Software Down the Drain
USB 3.0 One Step Closer to Reality
Would-Be Linux Contributors May Get a Leg Up

Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element
Setting Up and Running Subversion and Tortoise SVN with Visual Studio and .NET
Java/JRuby Developers, Say Open 'Sesame' to the Semantic Web
Interpreting Images with MRDS Services
DevXtra Editors' Blog: Executives Avoiding Cloud Computing in Droves
Q&A with James Reinders on the Intel Parallel Studio Beta Program
The Pros and Cons of Outsourcing Enterprise Emails
Hosting Options: Shared or Dedicated Server

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers