Custom JSTL Label Development

Source: Internet
Author: User
Tags event listener tld

Custom tags can be used in JSP pages starting with jsp1.1, and using custom tags not only enables code reuse, but also makes JSP code more concise. The Simpletag interface and its implementation class Simpletagsupport are also added to the Jsp2.0 label extension API.

tag Extension API hierarchy:

An explanation of the TLD file elements:

Each custom label must be declared in the TLD file, and theTLD file is simply an XML file. The root element is <taglib>, which contains one or more <tag> tags, which are used to declare custom labels. only the <tlib-version> elements in the <taglib> element are required, and others are optional.

Elements in a TLD file

Label

Meaning

<taglib>

TLD root element of the file

<tlib-version>

The version of this tag library

<jsp-version>

The JSP version that this tag library relies on .

<short-name>

<description>

<display-name>

<icon>

<uri>

<validator>

<listener>

<function>

This tag library is a preferred or recommended prefix when using tags in a JSP. Of course you can totally ignore this suggestion .

Description information

A short name that the graphical tool can display

Icons that can be displayed by the graphics tool

Specifies the URI that uses the label in the tag library

taglibraryvalidator Information about the library

Specifying Event Listener Classes

Define a function that is used in El

<tag>

Define a label

Child elements of the <tag> element

Label

Meaning

<description>

<display-name>

<icon>

<name>

Specify information for the label

A short name that the development tool uses to display

Icons that can be used by development tools

Label name

<tag-class>

<tei-class>

The name of the Java label Processor class. Note that this is the fully qualified name of the processor class, such as Com.xx.tag.TableTag

An optional subclass of the Javax.servlet.jsp.tagext.TagExtraInfo class

<body-content>

<variable>

<example>

The contents of the body portion of this label. The value can be scriptless\tagdependent\empty, default is empty

Defining script Variable Information

Optional informal description of using the label example

<attribute>

Metadata that contains a property of this label

Child elements of <attribute>

Label

Meaning

<description>

<name>

Text information about the description

the name of the property used in the JSP tag

<required>

<rtexprvalue>

<type>

Specifies whether the property is mandatory or optional, and the default is false, which indicates that the property is optional. If the value is true, the JSP page must provide a value for the property. Possible values true,false,Yes,no

Specifies whether the property can accept the value of the request-time expression, which defaults to false, indicating that the value of the expression cannot be accepted when requested. Possible values:true,false,Yes,no

The data type of the property , which can only be used when <rtexprvalue> is set to true. It specifies the return type of the property expression (<%=%>) when the request is used. Default string

instance (inheriting Simpletagsupport class mode):

Write a datetag tag to output the system time. <c1:date/>, output format : 2011 November 9th

Step1: Write a Java class, inherit the Simpletagsupport class

Step2:override Dotag () method, in which the corresponding processing logic is implemented

Package MyTag;

Import java.io.IOException;

Import Java.text.SimpleDateFormat;

Import Java.util.Date;

Import javax.servlet.jsp.JspException;

Import Javax.servlet.jsp.JspWriter;

Import Javax.servlet.jsp.PageContext;

Import Javax.servlet.jsp.tagext.SimpleTagSupport;

public class DateTag extends simpletagsupport{

@Override

public void Dotag () throws Jspexception, IOException {

PageContext CTX = (PageContext) getjspcontext ();

JspWriter out = Ctx.getout ();

SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month dd day ");

Out.println (Sdf.format (New Date ()));

}

}

step3: In the . tld file, describe the label

<?xml version= "1.0" encoding= "UTF-8"?>

<taglib xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"

version= "2.0" >

<tlib-version>1.1</tlib-version>

<short-name>c1</short-name>

<uri>http://www.tarena.com.cn/mytag1</uri>

<tag>

<name>date</name>

<tag-class>mytag. Datetag</tag-class>

<body-content>empty</body-content>

</tag>

</taglib>

step4: Importing tags using taglib (jsp)

<% @taglib prefix= "C1" uri= "Http://www.tarena.com.cn/mytag1"%>

<c1:date/>

Custom JSTL Label Development

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.