Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I c:set an int in my JSP?  RSS feed

 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use an int (or Integer) in my JSP to act much like a counter inside a <c:forEach> loop. I'm trying to do something like this:



But it's not working. I'm getting an exception:

"foo.jsp(49,4) According to TLD or attribute directive in tag file, attribute var does not accept any expressions"

Where line 49 is noted in the above code snippet.

How can I use a simple int in my JSP?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var doesn't accept expressions. Your problem really has nothing to do with using integers. If you want to set a variable named counter the syntax would be...



That said, would perhaps the following form of forEach achieve your goal better?


[ August 31, 2005: Message edited by: Bear Bibeault ]
 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Za! How did I miss that... Thanks for the reply Bear.

Also thanks for the alternate suggestion. In my case though, it's a bit more complicated, and I need my <c:forEach> index for something else. If you're interested,.. I've got this big list of items I'm looping through:

for example, an alphabetized list of names:


and the page I'm rendering has a fixed number of columns of text to place those items in -- that is, say, 4 columns for each letter. After we're done with the A's, put in a <div> and move onto the B's. And so on.

The "counter" variable is there to tell me how many items I've written in a given column (i.e. "you've written 4 A's so far") -- when it hits some predetermined number (like, say, 5 items for this column), extra logic comes in to end the current column and start a new one.

Thanks again,
---John
[ August 31, 2005: Message edited by: John M. Gabriele ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, so you need to maintain your own internal counter. That's cool.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you consider the varStatus attribute?
That keeps a record of the count for you ( as well as a few other things)

 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Did you consider the varStatus attribute?

Hi Stefan. I'd used varStatus in the past to tell whether I was at the last iteration ( <c:if test="${status.last}"> ), but forgot that I could also get a count from it. Thanks. Incidentally, I notice from reading some docs that status.count gives you a 1-based, not 0-based, count.
[ September 01, 2005: Message edited by: John M. Gabriele ]
 
Daniel Rhoades
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW - The index attribute of a varStatus variable also gives you the current increment
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!