Erwin DSouza wrote:

> Hi Hans,
>
> Sorry to bother you again with this old thread, but I
> hadn't thought of all the implications of your
> suggestion until now.
> I was asking about how to avoid attribute-name clashes
> in nested tags, and you suggested using:
>
>> The easiest solution is to let the page athor specify
>> the
>> variable name:
>>
>>   <!-- Create variable "foo" -->
>>   <mytags:foo var="foo">
>>     <!-- Create variable "bar" -->
>>     <mytags:foo var="bar">
>>        ...
>>     </mytags:foo>
>>   </mytags:foo>
>>
>> If you don't need to access the value through a
>> scripting...
>
>
>> If you *do* need to access the value through scripting
>> code,
>> you can tell the container to use the "var" attribute
>> value for
>> the scripting variable using either a TagExtraInfo
>> class (JSP 1.1
>> and JSP 1.2) or using the <variable> element in the
>> TLD (JSP 1.2).
>
> I *do* need to access the attributes in scripting code.
> But how do I add the new variable dynamically to the
> TagExtraInfo class? How does it help if I add "var" to
> the TEI?
> (I need foo and bar to be variables that I can access
> in my scripting code)
> Is this possible?


Yes it is. As to how, read a book, like mine :-) Just kidding,

even though I do recommend that anyone doing JSP development
read at least the specification. There are many things that you
just can't figure out just by looking at the APIs.

Anyway, here's a TEI that tells the container to create a variable
with the name supplied by the page author as the "id" attribute
value (you should use "id" when you create a scripting variable,
following the convention from <jsp:useBean>; "var" is a name convention
established by JSTL for variables saved only in a JSP scope):

   public class MyTagTEI extends TagExtraInfo {
     public VariableInfo[] getVariableInfo(TagData data) {
       return new VariableInfo[] {
         new VariableInfo(data.getAttributeString("id"),
           "com.mycompany.SomeType",
           true,
           VariableInfo.AT_END)
       }
     }
   }

The variable is of type "com.mycompany.SomeType" and made available
after the closing tag for the action element (AT_END).

If you use a JSP 1.2 container, you can replace the TEI with this
declaration in the TLD:

   <tag>
     <name>myTag</name>
     <tag-class>com.mycompany.MyTag</tag-class>

     <variable>
       <name-from-attribute>id</name-from-attribute>
       <variable-class>com.mycompany.SomeType</variable-class>
       <declare>true</declare>
       <scope>AT_END</scope>
       <description>This variable contains ...</description>
     </variable>
     ...
   </tag>


Hans

--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
JavaServer Pages        http://TheJSPBook.com

===========================================================================
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://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to