| Contents | Prev | Next |
|
pageContext.setAttribute(name, value, scope) or pageContext.setAttribute(name, value) methods. Typically an attribute passed to the custom tag specifies the name of the scripting variable object; this name can be retrieved by invoking the attribute's get method described in "Defining Attributes in a Tag Handler".
If the value of the scripting variable is dependent on an object present in the tag handler's context it can retrieve the object using the pageContext.getAttribute(name, scope) method.
The usual procedure is that the tag handler retrieves a scripting variable value object, performs some processing on the object, and then sets the scripting variable's value using the pageContext.setAttribute(name, object) method.
The scope that an object can have is summarized in Table 3. The scope constrains the accessibility and lifetime of the object.
In addition to setting the value of the variable within the tag handler, you must define a class derived from TagExtraInfo that provides information to the JSP container about the nature of the variable. A TagExtraInfo must implement the method getVariableInfo to return an array of VariableInfo objects containing the following information:
The JSP container passes a parameter called data to the getVariableInfo method that contains an attribute-value tuples for each of the tag's attributes. These attributes can be used to provide the VariableInfo object with a scripting variable's name and class.
Recall the scripting variable example described in the first section:
The object retrieved from the JNDI lookup is stored as a page context attribute with the name of the scripting variable.<tlt:lookup id="tx" type="UserTransaction" name="java:comp/UserTransaction" /> <% tx.begin(); %>
public LookupTag extends TagSupport {
private String type;
private String name;
public int doStartTag() {
return SKIP_BODY;
}
public int doEndTag() throws JspException {
try {
InitialContext context = new InitialContext();
Object obj = (Object)context.lookup(name);
pageContext.setAttribute(getId(), obj);
} catch(javax.naming.NamingException e) {
throw new JspException("Unable to look up " + name
+ " due to " + e.getMessage());
}
return EVAL_PAGE;
}
}
The scripting variable tx is defined in the following tag extra info class. Since the name (tx) and class (UserTransaction) of the scripting variable were passed in as tag attributes, they are retrieved with the data.getAttributeString method and used to fill in the VariableInfo constructor. To allow the scripting variable tx to be used in the rest of the page, the scope of tx is set to be AT_END.
public class LookupTagTEI extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData data) {
VariableInfo info1
= new VariableInfo(
data.getAttributeString("id"),
data.getAttributeString("type"),
true,
VariableInfo.AT_END);
VariableInfo[] info = { info1 } ;
return info;
}
}
teiclass Element
TagExtraInfo class defined for each scripting variable must be declared in the tag library descriptor as follows:
<tag> ... <teiclass>LookupTagTEI<teiclass> </tag>