Java Learning Notes-Custom Tags 1 (39)

Source: Internet
Author: User
Tags tld

In real-world development, such as a large number of JSP scripts in order to simplify JSP, we need to use standard tag libraries and El expressions, but the tags provided in the new tag library are limited and cannot fully meet the needs of development. such as: pagination. Therefore, you need to learn how to customize your own tag library.

If you want to implement a custom label, you need the following steps:

    1. Writing label processing classes

Need to inherit or implement related classes or interfaces

    1. Write a label description file

The file is an XML file and must be placed in the Web-inf directory of the Web site

    1. Introduce tags into the JSP and use the

Use the TAGLIB directive to introduce a tag library, which is then used.

Custom Label class System

Learn more about the next few classes and interfaces:

---| Jsptag interface

This interface is a typical markup interface. The primary markup class that implements the interface can handle the label. Seralizable

----| Tag interface

The interface mainly describes the common characteristics of the label processing class, but the class that implements the interface cannot handle the label body, which defines the communication protocol between the label processing class and the JSP page. It also provides life-cycle methods such as automatic execution at the beginning and end of a tag.

------| TagSupport class

is primarily responsible for handling the properties of the label.

-------| Bodytagsupport class

The main type of the class is the label body that handles the label.

Experience

1. Processing classes

 Public classHellohanler implements Tag {PrivatePageContext PageContext =NULL; //execution at the end of the label     Public intDoendtag () throws Jspexception {return 0; }    //Execute when the label starts     Public intdoStartTag () throws Jspexception {//output A Hello message to the pageJspWriter out=pagecontext.getout (); //Output InformationString info ="Hello custom Tag"; Try {             out. Write (info); } Catch(IOException e) {e.printstacktrace (); }        return 0; }    //get its parent tag     PublicTag getParent () {return NULL; }    //Release     Public voidrelease () {}//setting the JSP context object     Public voidsetpagecontext (PageContext pc) { This. PageContext =pc; }    //Set Parent Tag     Public voidsetParent (Tag t) {}}

2. Description file

<?xml version="1.0"encoding="UTF-8"?><taglib xmlns="Http://java.sun.com/xml/ns/javaee"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version="2.1"> <!--2. Write a tag library profile-<tlib-version>1.0</tlib-version> < Short-name>jnb</ Short-name> <tag> <name>hello</name> <tag-class>cn.itcast.test.hellohanler</tag-class> <body-content>empty</body-content> </tag></taglib>

3. Introduction

<% @taglib uri="/web-inf/test.tld" prefix="jnb"%>     <br/><jnb:hello/>
JSP1.2 for Custom Label development

Customize the label for a realistic date.

1. Implement a label processing class that can handle label properties

 Public classShowdate extends TagSupport {//to make it easier to get properties, you can define and provide a get and set method directly in the processing class with the same name as the property variable    PrivateString pattern;  PublicString Getpattern () {returnpattern; }     Public voidSetpattern (String pattern) { This. Pattern =pattern; }    //Automatic execution when the label starts     Public intdoStartTag () throws Jspexception {//Create Date ObjectDate Date =NewDate (); //creating a formatted objectSimpleDateFormat format =NewSimpleDateFormat (Getpattern ()); //FormattingString str =Format.format (date); //Get JSP Context objectPageContext PageContext = This. PageContext; //gets the out stream of the JSPJspWriter out=pagecontext.getout (); //Output        Try {             out. Write (str); } Catch(IOException e) {e.printstacktrace (); }        returnSuper.dostarttag (); }}

2. Description file

<taglib? Tag library description file root element xmlns="Http://java.sun.com/xml/ns/javaee"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version="2.1"> <!--2. Write a tag library profile-<tlib-version>1.0</tlib-version>? Specify the version of the tag library (required)< Short-name>jnb</ Short-name>? Specify the abbreviation for the tag library (required)<tag>? Specify a label to start with<name>showdate</name>? label name<tag-class>cn.itcast.custom.showdate</tag-class>? Specify label handling classes<body-content>empty</body-content>? Specify the tag body, JSP (with) empty (NO)<attribute>? Description Property<name>pattern</name>? Property name<required>true</required>? Description information for a property<rtexprvalue>true</rtexprvalue>? Description information for the property value</attribute> </tag></taglib>

3. Introduction and use

<% @taglib uri="/web-inf/date.tld" prefix="date"%>     <date:showdate pattern="yyyy mm month DD Day a E"/>

Implement custom labels with label bodies

1. Label Processing class

 Public classShowdatebybody extends Bodytagsupport {//to make it easier to get properties, you can define and provide a get and set method directly in the processing class with the same name as the property variable    PrivateString pattern;  PublicString Getpattern () {returnpattern; }     Public voidSetpattern (String pattern) { This. Pattern =pattern; }    //Automatic execution when the label starts     Public intdoStartTag () throws Jspexception {//Create Date ObjectDate Date =NewDate (); //creating a formatted objectSimpleDateFormat format =NewSimpleDateFormat (Getpattern ()); //FormattingString str =Format.format (date); //Get JSP Context objectPageContext PageContext = This. PageContext; //gets the out stream of the JSPJspWriter out=pagecontext.getout (); //get tags to mentionBodycontent BODY = This. Getbodycontent (); String Tag_body=body.getstring (); STR="<font color= ' Red ' >"+tag_body+"</font>"+str; //Output        Try {             out. Write (str); } Catch(IOException e) {e.printstacktrace (); }        returnSuper.dostarttag (); }}

2. Description file

  <tag>      <name>showdate2</name>    <tag-class> cn.itcast.custom.showdatebybody</tag-class>    <body-content>JSP</body-content>    <attribute>        <name>pattern</name>        <required>true</required>        <rtexprvalue>true</rtexprvalue>    </attribute>  </tag>

3. Introduction and use

<date:showdate2 pattern= "YYYY-MM-DD" > System time:</date:showdate2>

Java Learning Notes-Custom Tags 1 (39)

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.