To create a simple tag:
- Created
Tag
Tag 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
,Fasthashmap
OrFasttreemap
.Fasthashmap
AndFasttreemap
From 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 implementTag
Interface. 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 directlyTag
Instead, the map-defining label defined by map inherits the tagsupport class. This class is implemented in a meaningful default methodTag
To make it easier to develop custom tags (see references for links to the tagsupport API documentation ).
By default, tagsupport implementsDostarttag ()
To make it returnSkip_body
Constant, indicating that the label body is not judged. In addition, by default,Doendtag ()
Method returnEval_page
It indicates that the JSP runtime engine should judge the rest of the page. Finally, tagsupport implementsRelease ()
, It setsPagecontext
And 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-INF
Directory and declare it in the web. xml file. They are generally used. TLD
The 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
.Taglib
DescribesTag Library-A group of tag/Tag handler pairs.
- Because we use JSP version 1.2, we need
Tlib-version
AndShort-name
Element.
Tlib-version
The element corresponds to the tag library version.
JSP-version
Corresponding to the version of JSP technology on which the tag library depends.
Short-name
The element defines the simple name of the tag library that IDE and other development tools can use.
Taglib
The element contains manyTag
Element. Each tag in the tag library hasTag
Element.
To be continued...