Taglib
Tags are defined and distributed in a structure called the tag library. A tag library is a set of metadata and classes:
1. Tag Processor: Java class that implements the custom tag function.
2. Tag additional information (TEI): provides edge information to JSP containers to confirm tag attributes and create variable classes.
3. Tag library Descriptor (TLD): an XML document that describes a single tag and the attributes of the entire tag library.
The tag processor and additional tag information must be located in a location that can be found in the JSP Container class loader. The tag library descriptor can be specified in the URL. The JSP1.1 specification requires the JSP Container to accept the label library of a JAR file with the packaging structure. TLD must be a file named taglib. tld in the/META-INF directory, and the JAR file is copied to the/WEB-INF/lib directory.
I. Tag implementation
1. Development steps
A. Define the tag name, attributes, declared variables, and tag body content.
B. Write the tag library descriptor TLD.
C. Compile the tag processor.
D. Use tags on the JSP page.
2. JSP page conversion steps in the JSP Container:
There are three forms of JSP pages: jsp files, java files, and class files.
A. command element , And Provides the conversion information to the JSP Container.
B. the HTML line is converted to the out. print () language name in sequence in the _ jspService () method.
C. The declaration of the script element is copied to the source code outside the _ jspService () method.
D. The expression of the script element is converted to the out. print () language name in sequence in the _ jspService () method.
E. The Scriptlet of the script element is copied to the _ jspService () method.
F. The behavior element is converted to the runtime logic code that executes its functions.
G. custom labels are extended to Java statements that call methods in the corresponding tag processor.
3. Tag conversion steps in the JSP Container:
A. JSP containers use the taglib command element to locate the tag library Descriptor and match the custom tags used in the page with the TLD.
B. Read the tag LIST OF THE logstore Descriptor and the class names related to each tag.
C. When you encounter a tag on the page, find a tag library related to the tag prefix with the specified name.
D. The container uses the tag structure information found in the TLD to generate a series of Java statements to complete the tag function.
Ii. Tag library Descriptor (TLD)
The tag library descriptor is an XML document describing the tag information of the entire tag library and each tag processor and its attributes in the library.
The DTD of the tag library descriptor consists of a simple It contains the following sub-elements.
Tag Information of the entire tag Library
The version number of the tlibversion tag library. It is a dotted decimal number, which consists of up to four groups of digits separated by decimal places.
The minimum version of the JSP specification required by the jspversion tag library. For example, JSP1.1
The abbreviation of the shortname tag library. JSP can use this name as the default prefix of tags in the library.
The unique uri element of the URI tag library. The typical URL location is from the location where you can download taglib.
Description of the info tag library.
Each tag processor and Its Attributes
Tags are added to the TLD to describe each tag that constitutes the library.
The name of the tag used together with the name prefix of the tag library is the unique tag ID of the JSP Container.
The full name of the tag processor class that implements the tag.
The full name of the teiclass tag additional information (TEI) class. The TEI class provides information about the tag processor creation variables and any validity verification of tag execution.
Bodycontent describes how the label processor uses the content of the label body. Three values are available:
Empty: indicates that the label body must be empty;
JSP: the script element is evaluated like the template and other tags.
Tagdependent: The body content is unblocked and written to BodyContent. Other script elements are in the source code form, rather than being interpreted by the JSP Container.
Manual readable descriptive information of info labels.
Attribute information encoded when tags are used. Defines the attributes of a tag.
Property name: name of the property.
True | false: whether the attribute is encoded at the position used by the tag.
True | false: whether the attribute value can be specified using an expression.
Iii. Tag Processor
The tag processor is a Java class that executes custom tag behaviors through a series of predefined methods called by JSP containers.
The tag processor implements the behavior of tags. The tag processor is a Java class.
1. How the tag processor works
A. Import the javax. servlet. jsp and javax. servlet. jsp. tagext packages.
B. Implement the Tag interface or BodyTag INTERFACE IN THE javax. servlet. jsp. tagext package. BodyTag is a sub-interface of a Tag.
C. inherit from the TagSupport class or BodyTagSuppoert class. These are the default implementations of the above interfaces.
D. Reload the public int doStartTag () throws JspException method.
2. interface and implementation of the Tag Processor
Javax. servlet. jsp. tagext. Tag is the most basic interface for implementing tags.
Javax. servlet. jsp. tagext. TagSupport is a specific class for implementing the Tag interface.
Generally, it is beneficial to inherit the tagSupport class without directly implementing the Tag interface. In addition to providing default implementation for all required methods, the pageContext object and support for nested labels are also saved.
The Tag Interface contains four constants, indicating the possible return codes of doStartTag () and doEndTag () methods.
When doStartTag () is returned, EVAL_BODY_INCLUDE indicates that the servlet should evaluate the TAG body.
SKIP_BODY when doStartTag () is returned, it indicates that the servlet should ignore the label body.
When doEndTag () is returned, EVAL_PAGE indicates that the rest of the page should be evaluated.
SKIP_PAGE when doEndTag () is returned, the rest of the page is skipped.
Tag Interface Method
The servlet generated by public void setPageContext (PageContext ctx) calls this method before requesting the processor to execute other tasks. The implementation class should save the context object so that it can be used in the tag lifecycle. From the context of the page, the tag processor can access all JSP implicit objects.
Public void setParent (Tag p) uses a Tag to locate the Tag on the Operation stack. Call after setPageContext.