Can someone please give me a very simple but comprehensive description of using the Tag Extra Info classes for Tag Handlers?

Alessandro A. Garbagnati

As a brief description we can say that javax.servlet.jsp.tagext.TagExtraInfo it's a very handy class that allows a Tag to provide scripting variables that can be used outside the tag class and inside the jsp page.

The first step is to create the tag extra info class. This is a very simple operation, that can be done extending the TagExtraInfo class and overriding the getVariableInfo() method. This method it's used for providing the information regarding the variable that will be declared:

package com.foo;

import javax.servlet.jsp.tagext.*;

public class MyTagExtraInfo extends TagExtraInfo {

  public VariableInfo[] getVariableInfo(TagData td) {

    VariableInfo[] retVal = new VariableInfo[2];

    retVal[0] = new VariableInfo("Name", "java.lang.String", true, VariableInfo.NESTED);
    retVal[1] = new VariableInfo("BirthDate", "java.util.Date", true, VariableInfo.NESTED);
  }
}
In the example two variable has been declared. Both will be new, both will be NESTED (that means valid only inside the start and the end tag). One is a String and the name is "Name", and the other is a Date and it is called "BirthDate".

Then insiede the Tag code we will initialize them, passing the needed values;
package com.foo;

import ...;

public class MyTag extends TagSupport {

  // some code

  public int doStartTag() throws JspException {

    String name = "ThisTag";
    Date birthdate = new Date();

    pageContext.setAttribute("Name", name);
    pageContext.setAttribute("BirthDate", birthdate);

    return EVAL_BOSY_INCLUDE;
  }

  // additional code

}
At this point, when the doStartTag will be executed it will take care of inserting the given values into the two variables.

In the tag library descriptor you can then add the required tag:
<tag>
  <name>MyTag</name>
  <tagclass>com.foo.MyTag</tagclass>
  <teiclass>com.foo.MyTagExtraInfo</teiclass>
  ...
</tag>
And finally you can use it inside your JSP:
<%@ taglib uri="/META-INF/MyTag.tld" prefix="test" %>

  // html code here

  <test:MyTag>
    Name is <%= Name %>, Birth Date is <%= BirthDate %>, 
  </test:MyTag>

  // additional html code here

This example it's obviously meaningless, but you can think of the big advantages of using the TagExtraInfo for an iterating tag that, for example, display values of a table, or that provide configuration parameters, and so on...

In the example we have seen 'NESTED' variables, whose life ends with the end tag. But the variables can be used after the end tag when they are declared VariableInfo.AT_BEGIN or VariableInfo.AT_END: you can read, for example, a color scheme, from a file, or a database and, then, set the color variable and use inside your page and all the page included with the <%@ page ... %> directive (not with jsp:include).

Note: Please excuse any syntax and coding error...

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.