scriptless webpage

Hello everyone,

I seek advice on implementing scriptless code into a web page for a vocabulary project. I nearly have a scriptless web page except I don't know the best way to to go about this one. I tried to use

<%=sa[${incr.count}%2]%>

but gives me the error: ']' expected.

here is the snippet I am having trouble with:

<table width="540" border="1" align="center">

<tr>

<th>

<div align="center"><b>word</b></div></th>

<th>

<div align="center"><b>meaning</b></div>

</th>

<th>

</th>

</tr>

<%

int incr=-1;

%>

<c:forEach var="vocab" items="${list}" varStatus="incr">

<%

String[] sa={"tdwhite","tdgrey"};

incr++;

%>

<tr>

<td class="<%=sa[${incr.count}%2]%>">

<div align="center"><a href="action.jsp?action=edit&id=${vocab.id}">${vocab.name}</a></div></td>

<td class="<%=sa[incr%2]%>">${vocab.description}</td>

<td class="<%=sa[incr%2]%>">

<div align="center" class="style1">look it up on <a href="http://dictionary.reference.com/browse/${vocab.name}">dictionary.com</a></div></td>

</tr>

</c:forEach>

</table>

Any advice is greatly appreciated. Thanks!

--Gregory

[2221 byte] By [Greg439a] at [2007-11-26 14:59:00]
# 1
look at creating tag libraries
CarrieHunta at 2007-7-8 8:47:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

JSTL and EL works best with JavaBeans, and the idea is to move as much work from the JSP page as possible. To this end, I would consider creating a JavaBean that works as a 'constants' type of data store. In this little sample the bean would need just a few methods.

package luke.steve.datastore;

public class ConstantsBean {

private static final String tableCellClassNamesEven = "tdwhite";

private static final String tableCellClassNamesOdd = "tdgray";

public String getVocabTableCellClassNameEven() {

return tableCellClassNamesEven;

}

public String getVocabTableCellClassNameOdd() {

return tableCellClassNamesOdd;

}

}

Of course, you could fill it with other values that could be usefull as well. On the JSP page, you would have:

<!-- /* First, import the JavaBean above */ -->

<jsp:useBean id="constants" class="luke.steve.datastore.ConstantsBean" scope="page" />

<!-- /* Then include the code and loop */ -->

<table width="540" border="1" align="center">

<tr>

<th><div align="center"><b>word</b></div></th>

<th><div align="center"><b>meaning</b></div></th>

<th></th>

</tr>

<c:forEach var="vocab" items="${list}" varStatus="incr">

<c:choose>

<!-- /* If at an even increment, get the even class name */ -->

<c:when test="${(incr.count mod 2) eq 0}">

<c:set var="${className}" value="${constants.vocabTableCellClassNameEven}" />

</c:when>

<!-- /* Otherwise get the odd one */ -->

<c:otherwise>

<c:set var="${className}" value="${constants.vocabTableCellClassNameOdd}" />

</c:otherwise>

</c:choose>

<tr>

<td class="${className}">

<div align="center"><a href="action.jsp?action=edit&id=${vocab.id}">${vocab.name}</a></div>

</td>

<td class="${className}">${vocab.description}</td>

<td class="${className}">

<div align="center" class="style1">look it up on <a href="http://dictionary.reference.com/browse/${vocab.name}">dictionary.com</a></div>

</td>

</tr>

</c:forEach>

</table>

Message was edited by:

stevejluke

Those smarter than me will probably do a better job - maybe with a resource bundle.

stevejlukea at 2007-7-8 8:47:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

You got a couple of problems

first: int incr=-1 and varStatus="incr"> are two different variables

if you like to work with int incr you need to do something like

<td class="<%=sa[incr%2]%>">

<% incr++;%> and you can display in jsp expression <%= XXXX %>

but if you like to use varStatus="incr" you need to use <c:out value="${incr.count%2}"/>

remember int XXXX and varStatus="XXXX" a totally different variables.

akulikova at 2007-7-8 8:47:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...