Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This

Custom Tag Attributes

Custom tags can have zero to many attributes, which are in the familiar name-value pairs format, separated by whitespace, and appear after the tag name, like this:

<mylib:mytag attr1="value1" attr2="value2"/>

Attributes provide a way for the custom tag user to pass information into the Tag Handler class at request time. Note also that if a custom tag attribute is given a name of "id", then it is special. If a tag with this ID attribute creates a runtime object, that object can be identified to other tags, for example, by the value of that attribute (see the section "Custom Tag with Body Content" later in this article).

Here is an example of a custom tag with attributes, one used to test Xalan-Java 2 with its birds example files (note that this should be all on one line; space constraints required showing the line's continuation by indented lines):

<bon:transform type="xalanVersion" inXML="..\\webapps\\bonForum\
     mldocs\\birds.xml" inXSL="..\\webapps\\bonForum\\mldocs\
     birds.xsl" outDoc="..\\webapps\\bonForum\\mldocs\
     birds.html"> </bon:transform>

All four attributes in this example are required because of the way they are described in the TLD (and because the Tag Handler class needs them). Another tag used in bonForum illustrates that this can be otherwise. Here are three different ways to add the outputDebugInfo action to a JSP in bonForum:

<bon:outputDebugInfo type="init"/>
<bon:outputDebugInfo force="yes"/>

To coordinate the tag handler of a tag that has attributes with the JSP document and its container, we need to add some attribute elements to the tag element in the TLD file. Abbreviating the actual info element content for simplicity, the outputDebugInfo tag element in the bonForum TLD file is as follows:

Outputs debug information.
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.