JSP tag handler learning summary -- simple tag

Source: Internet
Author: User
To create a simple tag:
    1. CreatedTagTag processing of the interface (javax. servlet. jsp. tagext. Tag)ProgramClass.
    2. Create a TLD file.
    3. Create attributes in the tag handler Java class.
    4. Define the attributes corresponding to the attributes defined in the tag handler Java class in the TLD file.
    5. Declare the scriptlet variable in the TLD file.
    6. ImplementationDostarttag ()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 isTaglib.TaglibDescribesTag Library-A group of tag/Tag handler pairs.
    • Because we use JSP version 1.2, we needTlib-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...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.