jeff luo wrote:

> Hi,  I want to create a tag that can declare a script
> variable in the jsp page,  I use the tag extraInfo
> class to declare the variable, it work fine when I
> know how many variable I want to declare in the jsp
> page.   But I have a problem need to solve,  the
> variable number defined in the extratInfo class
> (VariableInfo[] ) is fixed, but I may call the tag
> many times in the jsp page(dynamic), how can I solve
> this problem? Can I define a dynamic list in the
> ExtraInfo class for the tag?  thanks!
>

How do you figure that the number of variables you can create is fixed?  Because
the return value is an array, you can create an array large enough for however many
variables you want to return.  For example, to return 3 variables this time:


public VariableInfo[] getVariableInfo(TagData data) {

    ... decide to return three new scripting variables this time ...

    VariableInfo info[] = new VariableInfo[3];
    info[0] = new VariableInfo(...);
    info[1] = new VariableInfo(...);
    info[2] = new VariableInfo(...);
    return (info);

}

Or, if you don't know ahead of time how many you need, consider this approach:

public VariableInfo[] getVariableInfo(TagData data) {

    // Accumulate the set of variables to be defined
    ArrayList list = new ArrayList();
    for (...) { // Loop through some stuff
        if (... we should create a new variable ...) {
            list.add(new VariableInfo(...));
        }
    }

    // Convert the accumulated list to an array and return it
    VariableInfo info[] = new VariableInfo[list.size()];
    return ((VariableInfo[]) list.toArray(info);

}


Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to