Using the Struts Logic Tags for Iteration


This section provides information about the following tag:

  • <logic:iterate> ” Loop over all the values of a collection and evaluate its contents

This section uses the Logic Iterate page at /StrutsTaglibs/LogicIterate.do . The rendered page is shown in Figure 14.4.

Figure 14.4. Logic Iterate page at /StrutsTaglibs/LogicIterate.do .

graphics/14fig04.gif

This page demonstrates a typical use of each of the tags. The source is shown in Listing 14.3.

Listing 14.3 JSP File Demonstrating Use of Struts Logic Tag Iterating over Contents ( LogicIterate.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ page import="java.util.HashMap" %> <html:html> <head> <title>Logic Iterate sample code</title> </head> <body bgcolor="white"> <h3>Logic Iterate sample code</h3> <p>This page provides examples of the following Struts LOGIC tags:<br> <ul> <li>&lt;logic:iterate&gt;</li> </ul> <%-- Variables used on this page --%> <% HashMap h = new HashMap(); String jameses[] = {"Joyce", "Thurber", "Kirk", "Cameron", "Monroe"}; String kevins[] = {"Spacey", "Bacon"}; String bruces[] = {"Willis", "The Shark", "Cockburn"}; h.put("James", jameses); h.put("Kevin", kevins); h.put("Bruce", bruces); request.setAttribute("givenNames", h); %> <%-- The following section shows iterate. --%> <logic:iterate id="gname" indexId="ind" name="givenNames">   <bean:write name="ind"/>. <bean:write name="gname" property="key"/><BR>   <logic:iterate id="lname" name="gname" property="value" length="4" offset="1">       -----<bean:write name="lname"/><BR>   </logic:iterate> </logic:iterate><P> <logic:iterate id="header" collection="<%= request.getHeaderNames() %>">    <bean:write name="header"/><BR> </logic:iterate> </body> </html:html> 

The logic:iterate Tag

The iterate tag is probably the most complicated one in the logic tag library, but also one of the most useful. At its heart, it's pretty simple. You specify a collection with the name or name and property attribute, and specify a bean to hold the individual members of the collection in id . The tag then loops over each member of the collection (which can either be an array, a Collection , Enumeration , Iterator , or Map ), setting the id to the element and evaluating its contents.

If the collection is a map, the id is set to an object of class java.util.Map.Entry . These objects have two values: a key and a value .

Sometimes you want to iterate over a computed value rather than a bean property. In that case, you can use the collection attribute, as in the example in which the header names are displayed.

The length attribute limits the total number of items that are evaluated, and the offset attribute enables you to skip over values at the beginning of the collection.

Finally, you can get access to a counter ( zero-based ) that lets you know which iteration you're on by using the indexed attribute.

If the value of an iteration is null, no page-scoped value for id is established on that iteration and the item is skipped .



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net