Two Laptop Bag
The moose likes JSP and the fly likes JSTL and c:if problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL and c:if problem" Watch "JSTL and c:if problem" New topic

JSTL and c:if problem

Miki Italiano

Joined: Mar 07, 2004
Posts: 7
I think I might already know what the problem is, but I wanted to share and see if anyone believes the same...
Here is a snippet of code in my results jsp page...
<td colspan=4>
<!-- set the end index of the current page -->
<c:set var="endIdx" scope="page">
<c:if test="${(startIndex+size) < total}">
<c ut value="${startIndex+size-1}"/>
<c:if test="${(startIndex+size) >= total}">
<c ut value="${total}"/>
<!-- display buttons when appropriate -->
<c:if test="${startIndex} > 1">
<html:submit property="submit">Prev</html:submit>
<c ut value="${startIndex}"/> to
<c ut value="${endIdx}"/> of
<c ut value="${total}"/>
<c:if test="${endIdx} < ${total}">
<html:submit property="submit">Next</html:submit>
Not pretty, but it works (until $total > 99).
$size = 15
if the output reads 1 to 15 of 75, the Next button shows up.
if the output reads 1 to 15 of 105, the Next button doesn't show up.
What I'm thinking is the problem here is that the JSTL c:if statement is comparing the values in the $vars as strings, not numbers. Because of this, my $endIndex < $total if statement is failing because 15 < 105 is false. If I could somehow compare the values as numbers, I would be ok, otherwise I think I have to left pad with zeros the variables to some fixed length before comparing them to one another. What do you think of this? Is there a better way with JSTL. I'm pretty new to this stuff.
Miki Italiano

Joined: Mar 07, 2004
Posts: 7
Adding "+0" to endIdx and total fixes the problem. It makes the auto type coersion think we're dealing with numbers again. Is there a better way?
<c:if test="${(endIdx+0) < (total+0)}">
<html:submit property="submit">Next</html:submit>
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63862

I have no way of testing this supposition where I am, but I suspect the problem stems from your initial declaration of

which probably sets up endIdx as an empty string, I'd try setting its initial value to zero.
[ March 27, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: JSTL and c:if problem
It's not a secret anymore!