Jstl Learning (2) custom tag Library

Source: Internet
Author: User
Tags return tag tld

The labels provided by jstl are very powerful, but only these labels cannot fully meet the actual development needs. When necessary, we can write our own labels by ourselves, you can even build your own tag library.

To develop a custom tag, you must first develop the class corresponding to the tag, and then write the tag description file TLD, and put this file under the web-info directory (in fact, it can be placed in another directory, but it needs to be in the web. xml configuration, for ease of use we usually put in the WEB-INF directory by default ). Finally, mark the location of the TLD file on the JSP page, and find the class for processing tags based on the description in the TLD file.

A picture is better than a thousand words

The following example uses a custom tag to output the current time on the page. The specific implementation code is as follows:

Tag processing class:

Package taglibs; import Java. text. simpledateformat; import Java. util. date; import javax. servlet. JSP. jspexception; import javax. servlet. JSP. pagecontext; import javax. servlet. JSP. tagext. tag; public class datetag implements tag {private pagecontext; private tag; @ overridepublic int doendtag () throws jspexception {try {date = new date (); simpledateformat dateformater = new simpledateformat ("mm DD, YYYY"); pagecontext. getout (). print (dateformater. format (date);} catch (exception e) {e. printstacktrace ();} return tag. eval_page ;}@ overridepublic int dostarttag () throws jspexception {return tag. skip_body ;}@ overridepublic tag getparent () {return NULL ;}@ overridepublic void release () {}@ overridepublic void setpagecontext (pagecontext) {This. pagecontext = pagecontext;} @ overridepublic void setparent (TAG parent) {This. tag = parent ;}}

Code parsing:

The preceding datetag class implements the tag interface. to process the body content, that is, to process the content between the start tag and the end tag, you must implement the bodytag interface. In this example, we only output the current time and did not process the body content, so we chose to implement the tag interface.

Because the label to be implemented here does not process the body content, you only need to implement the doendtag method to obtain the current system time in this method and output it in the specified format.

Tag library description file:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" ><taglib>  <tlib-version>1.0</tlib-version>  <jsp-version>1.2</jsp-version>  <short-name>dateTagExample</short-name>  <uri>/mytags</uri>  <tag>    <name>date</name>    <tag-class>taglibs.DateTag</tag-class>    <body-content>empty</body-content>  </tag></taglib>

Code parsing:

After the tag function handler is developed, you need to configure the tag library, that is, to configure reference to the tag function implementation class. The JSP Engine will find the tag configuration file based on the tag reference on the page, then, find the Java class that implements the tag function based on the configuration file, and call the functions in the class.

Row 4th indicates that the version of the tag library is 1.0.

Row 5th specifies that the JSP version required by this tag library is 1.2.

6th the behavior label starts with a descriptive name (not required for subsequent calls ).

Row 7th defines the URI that uniquely identifies the tag library as/mytags.

8th ~ 12 lines describe a tag in this tag library. The tag name is date, that is, the access name on the JSP page is date. The taglibs function processing class is used for this tag. datetag, which specifies that the label has no body content, that is, it cannot contain other content between the start tag and the end tag. A label is described here. To describe Multiple labels, you can add a <tag> node.

Test page:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib prefix =" mytags "uri ="/WEB-INF/datetag. TLD "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

Code parsing:

The description file of the above tag library needs to be placed in the WEB-INF folder of the current application project, then you can use this tag library through the above line of code. The reference to the tag library is indicated in the line, where URI specifies the location of the tag library description file as/WEB-INF/datetag. TLD, and prefix specifies the prefix to access this tag library.

<Mytags: Date/> in row 3rd references the date tag in the tag library. The running effect is as follows:

Display Effect:

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.