Recipe3.3.Displaying Indexed Properties


Recipe 3.3. Displaying Indexed Properties

Problem

On a JSP page, you need to access data from an indexed property of an object.

Solution

Use bean.property[index] to access the indexed value, as shown in Example 3-1.

Example 3-1. Accessing indexed properties
<@taglib uri=http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <ul>   <li><bean:write name="foo" property="bar.baz[0]"/></li>   <li><bean:write name="foo" property="bar.baz[1]"/></li>   <li><bean:write name="foo" property="bar.baz[2]"/></li> </ul>

JSTL supports access to indexed properties, as shown in Example 3-2.

Example 3-2. Accessing indexed properties (JSTL)
<@taglib uri="http://java.sun.com/jstl/core" prefix="c"%> <ul>   <li><c:out value="${foo.bar.baz[0]}"/></li>   <li><c:out value="${foo.bar.baz[1]}"/></li>   <li><c:out value="${foo.bar.baz[1]}"/></li> </ul>

Discussion

Indexed properties are one of the most misunderstood aspects of the Struts tags. An indexed property is a JavaBean property that represents a set of values, not a single scalar value. Indexed properties are accessed using a getter method of the following form:

public Foo getSomeProperty (int index) { ... }

Likewise, indexed properties are set using a setter method of this form:

public void setFoo(int index, Foo someProperty) { ... }

Consider a JavaBean representing a calendar. The CalendarHolder class shown in Example 3-3 has a nested property representing the months in a calendar named monthSet.

Example 3-3. Calendar JavaBean
package com.oreilly.strutsckbk; public class CalendarHolder {          private MonthSet monthSet;     public CalendarHolder( ) {         monthSet = new MonthSet( );     }          public MonthSet getMonthSet( ) {         return monthSet;     } }

The MonthSet class, shown in Example 3-4, is a class that has an indexed property, month representing the month names ("January," "February," and so forth).

Example 3-4. Class with indexed property
package com.oreilly.strutsckbk; public class MonthSet {          static String[] months = new String[] {             "January", "February", "March", "April",               "May", "June", "July", "August",               "September", "October", "November", "December"     };          public String[] getMonths( ) {         return months;     }     public String getMonth(int index) {         return months[index];     }          public void setMonth(int index, String value) {         months[index] = value;     } }

The goal is to access the indexed property month of the monthSet property of the CalendarHolder instance in a JSP page as shown in the following snippet from a JSP:

<jsp:useBean  /> <ul>     <li><bean:write name="calendar" property="monthSet.month[0]"/></li>     <li><bean:write name="calendar" property="monthSet.month[1]"/></li>     <li><bean:write name="calendar" property="monthSet.month[2]"/></li>     </ul>

If the specific indexed property to display was determined dynamicallythat is, the index to use was set using a JSP scripting variableyou would need to use scriptlet to generate the property value as follows:

You have selected month number <bean:write name="monthIndex"/>:  <bean:write name="calendar"          property='<%= "monthSet.month[" + monthIndex + "]" %>'

Using the scriptlet approach makes for an extremely hard to read and even harder to maintain JSP page. If you were using JSTL, however, this becomes much cleaner:

You have selected month number <c:out value="${monthIndex}"/>:  
<c:out value="${calendar.monthSet.month[monthIndex]}"/>

More commonly, indexed properties are accessed dynamically in a loop. Say you want to display the list of months using the Struts logic:iterate tag. This tag iterates over Collections and arrays. Here's how you would display all the months in an ordered list:

<ol> <logic:iterate  name="calendar" property="monthSet.months">     <li><bean:write name="monthName"/></li> </logic:iterate> </ol>

Again, JSTL can be used as an alternative. The JSTL c:forEach tag is a bit easier to use than the Struts logic:iterate tag. Here's how you would generate the same ordered list using JSTL:

<ol> <c:forEach var="monthName" items="${calendar.monthSet.months}">     <li><c:out name="${monthName}"/></li> </c:forEach> </ol>

See Also

Problems come in when you need to create form fields that correspond to indexed properties using the Struts html tags. Recipe 3.4 addresses these particular issues. Recipe 3.5 provides more details on using indexed properties in JSTL looping constructs.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

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