Custom Label Development

Source: Internet
Author: User

I. Overview

The tag library technology in JSP allows us to customize our own tags, which is actually a Java class that implements a specific interface, encapsulates some common functionality, and the runtime tag is replaced by the corresponding code. This article will introduce and summarize the development of custom labels.

Second, Tag Library

To develop a custom tag library, the core is to write the label processor class, all the label processor classes to implement the Jsptag interface. Labels are also divided into traditional labels and simple labels. Implements the relationship between the main interfaces in the tag library and the inheritance of classes.

   

The following four basic functions can be achieved by using tags:

    1. Controls whether the page content (label body) is output
    2. Use tags to control whether the entire JSP outputs
    3. Control tag Body Repeat execution
    4. Modify JSP page content with tags
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%@ taglib uri="/com.cn "prefix=" com "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

The following methods for implementing the above functions are described in these two tabs:

Traditional Tags:
    • Controls whether the page content (label body) is output

First, the JSP code is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%@ taglib uri="/com.cn "prefix=" com "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

To control whether the contents of the label body (AAAA) are output, write the appropriate label processing class:

public class Demo2 extends tagsupport{public    int doStartTag () throws Jspexception    {/        *             return Eval_body_ INCLUDE, which indicates that the label body to be executed             returns Skip_body, indicating that the tag body is ignored        */        return eval_body_include;}       }

    • Use tags to control whether the entire JSP outputs

The corresponding JSP code is:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%@ taglib uri="/com.cn "prefix=" com "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><com:demo3/> <!--custom label with empty tag body-->

To control whether the entire JSP page is output, the corresponding label processor class is as follows:

public class Demo3 extends bodytagsupport{public        int Doendtag () throws Jspexception    {/        *             return value = Eval_ page, which indicates that the remainder of the JSP page will continue execution             with a return value of skip_page, which means ignoring the remainder of the JSP page         */        return skip_page;}    }

    • Control tag Body Repeat execution

This label processor class implements the Iterationtag interface, which adds a method and a return-worthy constant to control the repetition of the label body.

The corresponding tag processor class code is as follows:

public class Demo4 extends tagsupport{    private int i;    @Override public    int doStartTag () throws Jspexception    {         i=0;        return eval_body_include;    }    public int doafterbody () throws Jspexception    {            i++;            if (i<5)                return eval_body_again;  Request repeated execution of label body            else                return skip_body;         Not executing tag body    }}

The processor class above will control the execution of the label body contents 5 times.

    • Modify JSP page content with tags (change the label body to uppercase output)

This label processor class implements the Bodytag interface, which adds a method and a return-worthy constant to control the repetition of the label body.

public class Demo5 extends bodytagsupport{public        int Doendtag () throws Jspexception        {            bodycontent bc= This.getbodycontent ();            String content=bc.getstring ();            Content=content.touppercase ();            Try            {                this.pageContext.getOut (). write (content);            }             catch (IOException e)            {                throw new RuntimeException (e);            }            return eval_page;}        }

Simple label:

The interface to be implemented for a simple label is Simpletag, and its label processor class has a life cycle of:

  

PS: Label processing instances of simple labels are not cached and reused, and each time a tag is encountered, the container creates a new instance of the label processor.

To implement the four features mentioned earlier, see the following sample code:

public class Demo1 extends Simpletagsupport  {    @Override public    void Dotag () throws Jspexception, IOException    {        jspfragment    jf=this.getjspbody ();        StringWriter sw=new StringWriter ();                Jf.invoke (SW);                String content = sw.tostring ();        Content=content.touppercase ();                This.getjspcontext (). Getout (). write (content);}    }

Third, Tag Library descriptor

After you have written the label processor class, you also need to configure the label's information in the tag library description file. A tag library descriptor is an XML document that contains attribute information for Tag name ' tag processor classes and tags. Its file name extension is TLD and the file is stored in the Meta-inf directory.

The sample code for a TLD file is as follows:

<?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 "> <description>a tag Library exercising simpletag handlers. </description> <tlib-version>1.0</tlib-version> <short-name>test</short-name> <u ri>/test.cn</uri> <tag> <name>demo1</name> <tag-class>com.test.simple.t Ag.         demo1</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>demo2</name> <tag-class>com.test.simple.tag.Demo2</tag-class> <body-conten t>scriptless</body-content> <attribute> <name>name</name> <requ    Ired>true</required>    </attribute> </tag> </taglib> 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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