> -----Original Message-----
> From: Gary McGath [mailto:[EMAIL PROTECTED]]
> Sent: Monday, February 03, 2003 9:14 AM
> To: [EMAIL PROTECTED]
> Subject: Custom tag life cycle
>
>
> The webapp which I am developing (see http://www.timeczar.com for
> details) uses a moderately complex custom tag library, and
> I've found the lifecycle of a TagSupport object to be very confusing.
>
> As I understand it, a TagSupport object may (but is not
> guaranteed to) be reused for subsequent occurrences of the
> same tag in a JSP. This means that attribute variables can't
> safely be initialized in the constructor, because they may
> not get reinitialized for subsequent occurrences of the tag.
> (Here I'm assuming that setter functions for attributes
> simply set an instance variable.)
>
> After some digging, I found that the recommended way to reset
> attribute instance varaibles is to use the doEndTag method.
> This probably doesn't work if a tag is nested within a tag of
> the same name, but I can live with that.
Actually I believe that containers would be required to use two
different instances when tags are nested. The instance can only be
reused for subsequent uses of the tag *after* the first one is closed.
But I would do the initialization in doStartTag rather than doEndTag.
The latter may not be called if an exception is thrown from within the
tag body.
>
> Doing this works fine in Tomcat. However, I recently ported
> my webapp to Resin and found that it doesn't work there.
> Here's a cut-back excerpt from my JSP:
>
> <caltags:eventset>
> <caltags:evtstartdate mode="date" length="m"/> -
> <caltags:evtenddate mode="date" length="m"/> </caltags:eventset>
>
> The class which implements eventset includes the tag body
> once for each event in the set. The evtstartdate and
> evtenddate tags are implemented by a class called DateTag,
> which extends TagSupport. Under Tomcat, DateTag.setMode and
> DateTag.SetLength get called once for each tag in each
> inclusion of the tag body. Under Resin, only two calls (one
> for each of the date tags) are made to each of setMode and
> setLength. If I clear the mode and length fields when I call
> doEndTag, then all occurrences of the date tags except the
> first take on their default attributes, which is not the
> behavior I want.
>
> Are both Tomcat and Resin within spec in implementing
> different behaviors here? If so, what is the correct point
> in the lifecycle to reset attribute values in a TagSupport object?
The spec allows the container to assume that the attributes of a tag
handler will be retained across invocations, so if there are multiple
identical invocations, the setter methods do not need to be called
again.
Here's what I'd recommend:
Initialize the mode and length instance variables to null at
construction time.
Do something like this in doStartTag:
String mode = this.mode;
if (mode == null) {
mode = DEFAULT_MODE;
}
...and repeat for length. Never modify the instance variables or call
their setters yourself -- let the container manage them.
--
Tim Moore / Blackboard Inc. / Software Engineer
1899 L Street, NW / 5th Floor / Washington, DC 20036
Phone 202-463-4860 ext. 258 / Fax 202-463-4863
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]