JSP Tag Library Introduction (4) turn

Source: Internet
Author: User
Tags tld
JS to build your first label
The following advanced guide describes how to extend a JSP to have its own HTML encoding capabilities. This encoding method replaces the HTML code with the < and > symbols and is easily extensible to perform other coding operations. The whole example is simple, but it does show you the basics of customizing tags.

Create JavaBean
Any reusable part of the code should be included in the JavaBean. This is a very important point. This allows you to reuse the code in other projects. Because any code placed in the label processor is not reused outside of the label, the reuse part of the code is separated from it. In our example, the HTML coding logic is very general, so we put them directly in the JavaBean, see Listing B.

Creating a label processor
The label processor uses the code in Listing C.

The code is simple, including:

Reads the text contained within the Open and close tabs.
Call the HTML encoding function.
Returns the results to the JSP page.
Create a Label descriptor
We need to describe our own labels so that the system knows what they mean. The TLD file is usually named after the label processor and is stored under the "/web-inf/" directory. The file name extension for this file is. TLD. Please refer to listing D.

Updating Web XML files
Now you can tell the JSP container to use the tag library. To do this you need to modify the Web.xml file: Add a taglib entry to register the tag library and assign a URI to the label. A URI is the only index on the Web site that is applied to this particular label. Since labels may also be used on different Web sites in the future, it is best to use the full URL and/or package name (package name) to ensure this uniqueness. See listing E for sample code.


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.