This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
My design is like below. The difficult part , for me, is the bodyContent. each body content will be filled in each cell. what time I should to evalue the body content ? Where should I place each object into pageContext's "aUser" ?
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 ]
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )
Joined: Mar 27, 2003
I forgot to mention above that this algorithm fills along the rows before going into the next columns. This actually makes the 'rows' attribute redundant, since the table will occupy as many rows as it has User objects to fill.
You could also implement a column-filling scheme, whereby columns are filled first before moving to the next column. This would make the 'column' attribute redundant. Unless I've missed the point, which is quite possible, I would suggest that it be legal to declare only one of the 'row' or 'column' attributes on the action - whichever is declared is the one which is fixed in size.
I can also see that because I've nullified the scoped attribute when there isn't a User for that box, your scripting code will throw a NullPointerException when you reach u.getUserId(), so you need to test for 'u' being null first. In my code, this body will not be written to 'out' unless 'u' is non-null anyway (but the body will still be executed and throw an NPE). A SimpleTag with embedded EL expressions would avoid this problem because we could choose whether or not to invoke the JspFragment depending on whether the scoped attribute is null or not, rather than having to evaluate the body first and find out later!
Joined: Dec 23, 2003
Thanks, Charles. you gave me a so detailed help.
After doing research, I finish my tag. I post my coding here. Only I am not sure how to clear the contents during a loop. My solution is below. Maybe we have a better one.