The last couple weeks have been spent working on a couple different Struts applications, all of which will be deployed on Tomcat 5 with Hibernate. I’m finding that working with Struts is an enjoyable experience, the framework encourages true separation of HTML presentation and business logic and model.
As someone working on all aspects of the application, one of the things I butt my head against pretty freqently are the limitations of the struts HTML & Logic tags. For example, an application that shows a listing of widgets commonly shows each row representing a widget in alternating colors… grey, white, grey, white, etc. The Struts tags, specifically the logic:iterate tag provides no mechanism for determining what the current row number is. There are a number of solutions out there, almost all of them suggest using scriplets to create a simple counter in the loop:
<%
String LIGHT_COLOR = "LightColor";
String currentColor = DARK_COLOR;
int i = 0;
%>
<logic:iterate id="product" name="products">
<%
if ( i % 2 == 0) {
currentColor = "BLACK";
} else {
currentColor = "WHITE";
}
i++;
%>
<tr ALIGN="center" class="<%=currentColor %>">
<td>
<bean:write name="product" property="label" />
</td>
</tr>
</logic:iterate>
Of course, the entire reason tags are used is so that you don’t have to use scriptlets, so it seems like a pretty ugly hack to me. Fortunately I’m using Tomcat 5, so I stumbled upon the JSTL forEach tag, which provides effectively the same functionality as the Struts iterate tag, with the benefit of an index and row status so that the above code becomes:
<c:forEach var="product" items="${products}" varStatus="status">
<c:choose>
<c:when test="${status.index % 2 == 0}">
<tr ALIGN="center" class="dark">
</c:when>
<c:when test="${status.index % 2 != 0}">
<tr ALIGN="center" class="light">
</c:when>
</c:choose>
</c:forEach>
which is a bit more simple, with the main benefit that you don’t have use any Java code.
Further, it seems that there is alot of overlap between JSTL and the Struts tag library, which isn’t a bad thing. I’m guessing that not everyone has the opportunity to use JSTL since it requires a web container that supports JSP 2.0, but for those who are deploying to JSP 2.0 web containers, how much do you use JSTL versus the Struts tags? Are you phasing out Struts tags for JSTL? Keeping with Struts tags to maintain a sort of purity to your application?