To create a simple tag:
- Created
TagTag processing of the interface (javax. servlet. jsp. tagext. Tag)ProgramClass.
- Create a TLD file.
- Create attributes in the tag handler Java class.
- Define the attributes corresponding to the attributes defined in the tag handler Java class in the TLD file.
- Declare the scriptlet variable in the TLD file.
- Implementation
Dostarttag ()Method. In the tag handler class, set the value to the scriptlet Variable Based on the attribute.
Here we will create a simple tag handler, which will instantiate a map. Developers who use this tag can specify the map type to be instantiated ――Hashmap,Treemap,FasthashmapOrFasttreemap.FasthashmapAndFasttreemapFrom Jakarta commons collection Library (for more information, see references ). Developers can also specify the tag range-page, request, session, or application range.
To write a tag handler, you must implementTagInterface. As mentioned above, this interface is used for simple tag handlers that do not manipulate the label body.
Now, you don't have to implement it directlyTagInstead, the map-defining label defined by map inherits the tagsupport class. This class is implemented in a meaningful default methodTagTo make it easier to develop custom tags (see references for links to the tagsupport API documentation ).
By default, tagsupport implementsDostarttag ()To make it returnSkip_bodyConstant, indicating that the label body is not judged. In addition, by default,Doendtag ()Method returnEval_pageIt indicates that the JSP runtime engine should judge the rest of the page. Finally, tagsupport implementsRelease (), It setsPagecontextAnd its parent element isNull.
After the tag handler is defined, you need to add the tag ing from the handler to the TLD file.
The role of a TLD file to a custom tag handler is like the role of a Web deployment descriptor to a servlet. The TLD file lists the mappings between tag names and tag handlers. Most of the data in this file is used for JSP page conversion. TLD files are usually stored in Web ApplicationsWEB-INFDirectory and declare it in the web. xml file. They are generally used. TLDThe extension ends.
The TLD file hasIntroduction (preamble)Here, the JSP technology version and the tag library used are identified. This introduction usually looks like this:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.2 // en"
Http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd>
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 1.2 </JSP-version>
<Short-Name> Map </short-Name>
Let's analyze these labels in more detail:
- The root element of the TLD file is
Taglib.TaglibDescribesTag Library-A group of tag/Tag handler pairs.
- Because we use JSP version 1.2, we need
Tlib-versionAndShort-nameElement.
Tlib-versionThe element corresponds to the tag library version.
JSP-versionCorresponding to the version of JSP technology on which the tag library depends.
Short-nameThe element defines the simple name of the tag library that IDE and other development tools can use.
TaglibThe element contains manyTagElement. Each tag in the tag library hasTagElement.
To be continued...