13. Custom Labels

Source: Internet
Author: User
Tags tld

---restore content starts---

1. Custom Label Introduction

JSP presentation layer needs to write a lot of JSP script code

In other JSPs to write the same code repeatedly, can not do the code reuse

JSP developers can create new tags to tailor the method to their actual needs----custom labels

Use of custom tags to implement code reuse for page presentation layers

A few common tags:

Custom labels for empty label bodies

<prefix:tagName></prefix:tagName>
<prefix:tagname/>
For example
<mytag:required></mytag:required>
<mytag:required/>

Custom tags with attributes:

-Custom tags can contain attributes

-< Prefix: Label Name property 1 = "Value 1" Property 2 = "Value 2"/>

For example:-<test:function user= "join" num= "/>"

Custom Tags:

Custom simple label custom standard label custom JSP tag file

2. Introduction and use of simple tags

The concept of a simple label:

JSP2.0 provides a simple label to quickly implement a custom label function
The simple label provides the Simpletag interface and the Simpletagsupport implementation class

To customize a simple label:

Writing a simple tag implementation class
Writing TLD files
Configuring a TLD file in Web. xml
Using Taglib in JSP pages

(1) write a simple tag implementation class

 Public  class simpletagexample          extends simpletagsupport{      publicvoid dotag ()   throws                       Jspexception,ioexception      {           getjspcontext (). Getout (). Print ("Simple tag!");}       }

(2) Writing tag library descriptor files

The tag library descriptor file contains information that instructs the JSP container to parse the JSP custom label
A tag library descriptor is an XML document that informs the tag library user about the function and usage of the tag library

<?xml version= "1.0" encoding= "Utf-8"?><taglib>  ...   <tag>    <name>,  ..... </name>    <tag-class, ...  . </tag-class>    <body-content>, ...  . </body-content>    <attribute> ...    </attribute>  </tag></taglib>
<taglib>    <URI>Http://localhost:8080/08-03/math</URI>    <tlib-version>1.0</tlib-version>    <jsp-version>2.0</jsp-version>    <Tag>        <name>sqrt</name>        <Tag-class>Com. MathTag01</Tag-class>        <body-content>Empty</body-content>        <Description>Send A math expression to the JSP</Description>    </Tag></taglib>

(3) Configuring TLD files in Web. xml

<jsp-config> JSP related Configuration
<taglib> define libraries that can be used in JSPs
<taglib-uri> The Tablib command that defines the uri,jsp page of the TLD file can be obtained by this URI to the TLD file
<taglib-location> where TLD files are located

<Jsp-config>      <taglib>    <Taglib-uri>Http://localhost:8080/08-03/math</Taglib-uri>    <taglib-location>/mathtag01.tld</taglib-location>      </taglib></Jsp-config>

(4) using Taglib in JSP pages

<prefix     uri = "..../math"%> ... /> ... ..

Simple Label Implementation method:

 Public  class simpletagexample          extends simpletagsupport{      publicvoid dotag ()   throws                       Jspexception,ioexception      {           getjspcontext (). Getout (). Print ("Simple tag!");}       }

13. 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.