Exporting Hashtable as scripting variable from Tag Handler
I would like to have a tag handler create a hashtable (after doing a lookup in some databases) and export the hashtable as a scripting variable to the jsp page that uses the tag. The idea is that the JSP author, who knows the field names used as keys for the hash, would be able to do things like <%= fields.get("lastname") %> to get (for example) the hashtable value for "lastname".
The tag handler iterates through a result set so that the hashtable is "reloaded" with different values on each iteration. This way there is hardly any java code in the jsp page. (The tag itself has an atribute which consists of the sql query to be executed by the tag handler.)
I wrote the Tag Extra Info class and defined "fields" as a Hashtable, and I have the tag handler do pageContext.setAttribute("fields", fields) after loading the Hashtable with values. The Java compiles BUT the jsp does not. I get "Undefined variable or class name: fields".
The examples I have seen usually involve reflection and more elaborate approaches. The above would meet my needs-- if only I could get it to work!
Do I need to defined "fields" in the tld file? I tried that too but it didn't help.
[1213 byte] By [
ZaitchikA] at [2007-9-26 2:43:16]

You need to write a class that extends TagExtraInfo class where you define your scripting variable.
Let say you have tag class called MyTag.
The you could do it like this:
import javax.servlet.jsp.tagext.*;
public class MyTagTEI extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData data){
VariableInfo[] info = {
new VariableInfo("fields","Hashtable", true, VariableInfo.AT_BEGIN) };
return info;
}
}
And then in your tag class MyTag:
Hashtable myHashtable = new Hashtable();
this.pageContext.setAttribute("fields", myHashtable);
You have also define your scriptin variables in taglib-tld.
<tag>
<name>mytag</name>
<tagclass>MyTag</tagclass>
<teiclass>MytagTEI</teiclass>
</tag>
Hopefully this helps!
Jysse
jysse at 2007-6-29 10:21:43 >

Well I tried that but in the jsp file my reference to "fields" (which is the name of the exported Hashtable variable) is unknown. (Error is: could not compile the jsp because "unknown variable or class name: fields".
Is there anything special I need to add to the jsp file (or to the tld file, other than the pointer to the correct tei class) to get it to recognize the exported variable? I tried various stuff to no avail. Basically I would like to allow the jsp writer to use something as simple as
<%= fields.get("lastname") %>
since they will know the field names that serve as keys in the hash. I want to avoid having to use beans in this application.
Much thanks for any additional help. BTW, I would be glad to email you the code if you have time. Maybe I am missing something obvious...
In the code that has provided,
try replacing...
new VariableInfo("fields", "Hashtable", true, VariableInfo.AT_BEGIN)
with...
new VariableInfo("fields", "java.util.Hashtable", true, VariableInfo.AT_BEGIN)
i.e. use the fully qualified class name java.util.Hashtable instead of just Hashtable