February 2005
Intermediate to advanced
528 pages
12h 53m
English
You need to build an HTML table where the rows alternate in color or style.
Example 4-6 shows a JSP page (struts_table.jsp) that uses Struts tags to display tabular data where the background colors alternate between orange and yellow.
Example 4-6. Alternating table row colors using Struts
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix= "bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix= "logic" %> <html> <head> <title>Struts Cookbook - Chapter 4 : Tables</title> <style> /* Even row */ .row1 {background-color:orange;} /* Odd row */ .row0 {background-color:yellow;} </style> </head> <body> <jsp:useBean id="weeklyWeather" class="com.oreilly.strutsckbk.ch04.WeeklyWeather"/> <table> <tr> <th>Day of Week</th> <th>Chance of Precipitation</th> <th>Expected Precipitation (inches)</th> </tr> <logic:iterate id="forecast" indexId="count" name="weeklyWeather" property="weekForecast"> <tr> <td class='row<%= count.intValue( ) % 2 %>'> <bean:write name="forecast" property="day"/> </td> <td class='row<%= count.intValue( ) % 2 %>'> <bean:write name="forecast" property="chancePrecip"/> </td> <td class='row<%= count.intValue( ) % 2 %>'> <bean:write name="forecast" property="rainfall"/> </td> </tr> </logic:iterate> </table> </body> </html>
Example 4-7 (jstl_table.jsp) shows a cleaner solution that uses JSTL EL instead of relying on JSP scriptlet. ...