Originally posted by Edward Chen:
I want to develop a jsp tag, like DisplayTag. Say, I have a List of users, now I want display a table 2x2, each cell is user id.
You didn't give an example of the output you required. Your requirement seems to imply that there are only 4 user ids - one in each cell of the 2x2 table? I'm going to assume that you have only 4, and want to put one in each cell, so the final (X)HTML looks like:
where userX is an example username. I assume also that your users are modelled by the User object (although you didn't explicitly say this).
So, I have the list:
where I have used generics (List<User>) as this is recommended J2SE 5.0 practice - if you don't use 5.0, simply remove the generics throughout.
Since you seem to want to use scripting expression rather than EL expression in the body, we're forced to use a classic tag, here implementing BodyTag.
The simplest way to achieve the filling of each cell along the rows as well as down the columns is to maintain a counter of the number of cells already filled, and do modular arithmetic to decide when to jump to a new row.
We might implement the handler like this (I've omitted all non-essential details from my discussion):
That should, I think, do the trick. As I say, this is skeleton code, and I hope there are no errors - but there could be a few because I haven't actually run it myself. If you have any problems, or the code doesn't work, repost with the exception stack trace and we'll try to work through it.
[ December 24, 2005: Message edited by: Charles Lyons ]