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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs