tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rs...@vgalleries.com (Richard Sand)
Subject Re: Custom attributes and nested tags
Date Mon, 29 Jan 2001 16:51:47 GMT
You must use different variables for the inner tag.

I wrote a nested tag that loops through a database result set, and I wrote
it so that I can have nested loops:  i.e. the outer loop goes through
records 1 through n by 3, and the inner loop goes through outer-loop to
outer-loop + 3.  To avoid the error you've encountered, I use a custom
variable attribute to define the variable value.

Sample JSP:

<ipa:loop varName="i" inc="3" start="1" end="15">
    <ipa:loop varName="j" loops="3">
        Count: <%=j%><br>
    </ipa:loop >
   <hr>
</ipa:loop>

This JSP sample will print the numbers 1 to 15 in sets of 3, with a line
between each set.

My TEI variableinfo is:
public VariableInfo[] getVariableInfo(TagData data) {
    String varName =
        (data.getAttributeString("varName") == null)
            ? "_count"
            : data.getAttributeString("varName");

    return new VariableInfo[] {
        new VariableInfo(varName, "Integer", true, VariableInfo.NESTED)
    };
}

You can see that if I do NOT specify varName, it defaults to "_count".  But
if I specify it, then that tag instance uses that variable name.  Thus I can
give each instance of the tag a different variable name.

And I do the following in my tag's doInitBody and doAfterBody methods:
    pageContext.setAttribute(getVarName(), new Integer(count));

That's about it.  It works fine.

Hope this helps.

Best regards,

Richard

----- Original Message -----
From: "Alex Tang" <altitude@airflash.com>
To: <taglibs-user@jakarta.apache.org>
Sent: Monday, January 29, 2001 2:47 AM
Subject: Custom attributes and nested tags


> Hi folks.
>
> I'm writing a Body tag which has the ability to nest itself: e.g.
>
>      <foo:mytag name="a">
>        <foo:mytag name="b">
>        </foo:mytag>
>      </foo:mytag>
>
> This works fine...except that the tag "foo:mytag" has attributes it sets
> with pageContext.setAttribute and a TagExtraInfo class.  These variables
> are defined as "nested".
>
> The nesting causes problems because I get compile errors when the second
> tag tries to define the variable since it has already been compiled in
> the first tag.
>
> Does anyone have any solutions to this problem?
>
> Thanks!
>
> ...alex...
>


Mime
View raw message