----- Original Message -----
Sent: Saturday, June 09, 2001 3:40
PM
Subject: Grid / Matrix Controls Made Easy
(src included)
Attached, you will find a couple of new tags I
wrote to extend the HTML tag library that comes with struts that will allow
you to write grid-style JSP code that looks like the following:
<%
int i = 0; %>
<logic:iterate id="item" name="inventoryForm"
property="items">
<tr>
<td></td>
<td class="smallFont"><html:gridtext name="item" property="name"
index="<%= i %>" maxlength="nameMaxSize"
size="30"/></td>
<td class="smallFont"><html:gridtext name="item"
property="description" index="<%= i %>" maxlength="descriptionMaxSize"
size="50"/></td>
<td class="smallFont" align="center"
valign="center"><html:gridcheckbox name="item" property="containerFlag"
index="<%= i %>"
value="Y"/></td>
</tr>
<% i++;
%>
</logic:iterate>
Here is a synopsis of what I did:
(1) added two tags called
"gridtext" and "gridcheckbox" that are based on html:text and html:checkbox
respectfully. Note: All of the others (ie. hidden, radio, select, etc.)
need to eventually be handled in a similar fashion but this is all I needed
for right now. I'll keep you posted on the rest as I create new gridXXX
tags. Note 2: this code has in no way been endorsed by Apache /
Struts. Therefore, buyer beware! This code could easily break in
future releases to struts.
(2) gridtext can now
optionally take an index field. If no index is given, the behavior of
gridtext degenerates to your vanilla html:text tag. If index is
specified, it will use the correct semantics for referring to your iterated
item in the collection.
(3) gridtext can now
optionally take a bean property name in place of a literal value for the
maxlength. I felt this is more convenient than the alternative (using
scriptlet). To use this style, make sure your underlying bean object (in
the above case its the "item" bean), has a member called getNameMaxSize()
which returns a string. Note: "size" still requires a literal, the bean
will not be consulted if you use a property name instead.
(4) gridcheckbox. Well,
let me tell you - a lot was changed in gridcheckbox! I began by copying
the code for checkbox tag over to gridcheckbox. I changed all private
declarations over to protected, removed the 'final' on the class declaration,
and fixed a few bugs along the way. Bugs included: (a) wrong target
package, (b) used new RequestUtil to get bean value (not really a bug), (c)
handle the value parameter correctly (in the 3.2.1 release, the value clause
was being confused with the data coming back in the form post.
gridcheckbox will really take your user-defined value and put it in the
"value" attribute. Also, "checked" logic will use your "value" attribute
and compare to datavalue posted from the form. If value is not
specified, the default value will be "true").
(5) gridcheckbox can now
optionally take an index field just like gridtext...
Also, note: this has not been qa'ed
properly. I wrote the code in under an hour and wham, here you go. If
there are bugs you find in the indexing logic, feel free to let me
know.
A few words on checkboxes:
As many of you know, if a form
is posted and no checkboxes are checked, html (and therefore struts) will not
fire the setter(s) for those checkbox properties. To solve this problem,
you should depend on your reset() method on your form to clear out all of the
checkbox values each time reset() is called.
To incorporate this code, do the
following:
(1) make a backup of your
struts-html.tld
(2) overwrite your
project-level struts-html.tld with the one attached.
(3) put GridTextTag.java &
GridCheckboxTag.java in your project.
(4) send me email to let me
know how you fared with them ;^) I'd like to hear from you.
cheers,
jeff