JSP2 Custom Labels

Source: Internet
Author: User
Tags tld

To develop a custom label in a JSP:

1 · Developing custom Label Processing classes

2 · Create a *.tid file that corresponds to a tag library for each TLD tag, with multiple tags for each tag library

3 · Using tags in JSPs


Considerations for developing Custom label classes:

1 · The custom label class should inherit a parent class Javax.servlet.jsp.tagext.SimpleTagSupport

2 · If a custom label class contains attributes, each attribute should have a corresponding Getter/setter method

3 · Overriding the Dotag () method This method is responsible for generating the contents of the page

Example:

public class Helloworldtag extends simpletagsupprt{

Overriding the Dotag () method

public void Dotag () throws jspexception,ioexception{

This.getjspcontext (). Getout (). Write ("Hello World" + New Date ())

}

}

Creating TLD Labels

TLD is the abbreviation for Tag library definition

We can copy a file template into Tomcat \webapps\examples\web_inf\jsp2 contains a jsp2-example-taglib.tld

Copy the file to the Web app's Web-inf path and make a simple modification

Example:

<!--define Labels--

Root node:

Tag


Using labels

Using labels requires two points:

1 • Tag library URL determines which label is used

2 · Label name: Determine which label to use


There are two steps to using a tag library:

1 · Import Tag Library import tag library using taglib compilation directives

<%@ taglib uri= "Http://tomcat.apache.org/debug-taglib" prefix= "MyTag"%>

URI: The URI of the tag library

Prefix: Short name

2 • Use tags; use custom tags in jsps

As we set the

<body-content>TAGDEPENDENT</body-content> define the label body as empty

We just need to call:

<mytag:log/> Prefix + label signature

JSP2 Custom Labels

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.