JSP note--7. Custom labels

Source: Internet
Author: User
Tags tld

JSP tags have the same format as HTML tags, which can be used instead of Java scripts in JSPs to introduce code. Before using a custom label, you need to create a *.tld file, each with a tag library that can contain a lot of custom tags.

Each tag must be implemented by an implementation class that inherits the Simpletagsupport class. The properties inside the class must have a get and set method. The Dotag method needs to be rewritten, in effect this method actually completes the work of the label.


Create a TLD file

When creating, it is best to find a ready-made TLD file, copy it and then modify the URI (the UTI can tell the JSP page that the prefix should be directed to which TLD file corresponds), and then define the tag under the tag child element.

    • Taglib has the following three child elements:

    • Tlib-version: Specifies the tag library implementation version, no use

    • Short-name: Default short name, also no use

    • URI: Tag Library's unique identifier

    • Dynamic-attributes: Specifies whether the label supports dynamic properties and is only required when defining dynamic property tags.

    • Tag: The place where the tag library is defined. Each tag can contain Name,tag-class and body-content. The following four values can be set in a body-content:

    1. Tagdependent: Specifies that the label handling class is responsible for handling the label body

    2. Empty: Specifies that the label can only be used as an empty label

    3. Scriptless: Specifies that the tag body can make static HTML elements, expression language, and cannot have scripts

    4. JSP: Specifies that a JSP script can be used (not a JSP in JSP2)


Using the Tag Library

Use the tag library to first import with taglib compile instructions, to write the UTI prefix. Use prefix Plus: Spike signature.


Labels with attributes

You need to use the attribute child element when configuring. Each property corresponds to a sttribute. Each attribute needs to be configured with the name (property name), required (is required), fragment (whether JSP script is supported). Once configured, you will be prompted to enter the specified attribute when you use it. Here you can compare a label to a method, and by writing a method name, you can call the method. Importing taglib is equivalent to importing a jar package. Writing a prefix is equivalent to telling the program that I want to use the method inside the package.


Label with label body

Such tags can be embedded in the inside of other content to complete such as logical operations, loops and so on. General requirements body-content for scriptless.


Dynamic Property Labels

The Dynamicattributes interface needs to be implemented, overriding the Setdynamicattribute class. When configuring, you need to use Dynamic-attributes to annotate dynamic properties. You can optionally add attributes and property values when using tags. It seems that the program will automatically determine = Front is the property name, if so, then go to the set method, if not, OK, this is a dynamically generated property, hurriedly to create a.

This article from "Fingertip Light Fly" blog, declined reprint!

JSP note--7. Custom labels

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.