JSTL Custom Tags (iii)

Source: Internet
Author: User
Tags return tag tld

The last article is about the core tags commonly used in Jstl, this article describes the use of JSTL custom tags. Remember when learning JS, we learned the jquery framework, at the same time learning to construct their own JS library, and then to call their own JS library, it is like according to their own needs of a cohesive function, we create a function, then call this function. In learning the Java EE Specification jstl tag section, we can also construct our own tag library according to our own needs, and then use the label we define.

Jstl comes with the label function is very powerful, but does not fully meet our needs, development tag first is the need to develop the label corresponding to the function class, and write functions, the function is what? Next, write the TLD that describes the tag and put the file in the web-inf/directory before you can raise the custom label on the JSP page.

Examples of custom Tags:

We need three steps to write a custom label:

1, the function of the label processing program

Java source code:

  1. Package taglibs;
  2. Import java.io.IOException;
  3. Import Java.text.SimpleDateFormat;
  4. Import Java.util.Date;
  5. Import javax.servlet.jsp.JspException;
  6. Import Javax.servlet.jsp.PageContext;
  7. Import Javax.servlet.jsp.tagext.Tag;
  8. Public class DateTag implements Tag {
  9. private PageContext PageContext;
  10. private tag tag;
  11. /** 
  12. * Tag initialization function.
  13. */
  14. public int Doendtag () throws jspexception {
  15. Try
  16. {
  17. //define the date.
  18. Date date = new Date ();
  19. //Date format
  20. SimpleDateFormat dateformater = new SimpleDateFormat ("yyyy year mm DD day");
  21. Pagecontext.getout (). Print (Dateformater.format (date));
  22. }catch (IOException e)
  23. {
  24. E.printstacktrace ();
  25. }
  26. return tag.eval_page;
  27. }
  28. /** 
  29. * Label start
  30. */
  31. public int doStartTag () throws jspexception {
  32. return tag.skip_body;
  33. }
  34. Public Tag getParent () {
  35. return null;
  36. }
  37. public void release () {
  38. }
  39. public void Setpagecontext (PageContext arg0) {
  40. this.pagecontext = arg0;
  41. }
  42. public void SetParent (Tag arg0) {
  43. This.tag = arg0;
  44. }
  45. }

Description of the above procedure section:

Our own definition of the datetag tag implementation of the tag interface, if you need to deal with the content of the body, that needs to deal with the content between the start tag and the end tag, we need to implement the Bodytag interface, we do not need to deal with the body content here, so choose to implement the tag interface.

Because the label to be implemented here does not handle the body content, it is only necessary to implement the Doendtag method, in which the current system time is removed and the format output is specified.

2. Label description file TLD file

After the label processing function is developed, the tag library needs to be configured, that is, to configure a reference to the tag function implementation class, the JSP engine will find the corresponding label profile according to the label reference in the page, and then find the Java class that implements the label function according to the configuration file, and invoke the function in it.

Select New file in Eclipse, select the file for new TLD type, as shown below:

which includes us. tld file name Name:datetag, file version number version:1.2, tag library version tlibversion:1.0, the tag library requires a JSP version of 1.2, understanding the tag library descriptive name Shortname,uri The URI that uniquely identifies the tag library is/mytags. Our code is as follows:

  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en"
  3. "Http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
  4. <taglib>
  5. <tlib-version>1.0</tlib-version>
  6. <jsp-version>1.2</jsp-version>
  7. <short-name>datetagexample</short-name>
  8. <uri>/mytags</uri>
  9. <tag>
  10. <name>date</name>
  11. <tag-class>taglibs. DateTag</tag-class>
  12. <body-content>empty</body-content>
  13. </Tag>
  14. </taglib>


<tag></tag> describes a tag in the tag library, where the name of the tag is date, that is, the name on the JSP page is date, and the function of this tag is taglibs. Datetag,<body-content></body-content> indicates that the label does not have a body content, that is, the start tag and end tag cannot contain other content.

In a tag library, you can include a set of labels, or just a label, in general, a set of functional similar tags in a tag library, can include a set of labels can also be described only a label, usually a set of similar labels placed in a tag library, with a description file, Just add the <tag></tag> node to it.

3. Call this tag in the JSP

First put this description file. The TLD is placed under the Web-inf folder and then used in the JSP page to use this tag library:

<%@ taglib uri= "/web-inf/datetag.tld" prefix= "Mytags"%>

The code looks like this:

  1. <%@ page language="java" import="java.util.*" pageencoding= "GB18030"%>
  2. <%@ taglib uri="/web-inf/datetag.tld" prefix="mytags"%>
  3. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">
  4. <html>
  5. <head>
  6. Custom Label Examples <br>
  7. </head>
  8. <body>
  9. <font size="3">
  10. To output a date using a custom label:
  11. <mytags:date/>
  12. </Font>
  13. </Body>
  14. </html>


Call Result Display graph:

4, JSP page does not use the label and the execution process of using the tag

As shown in the following:


This finally understand the JSTL, is the JSP page embedded Java code to wear a layer of clothing, but this "coat" can be directly in the browser to run, closer to the interface production display requirements, the original Java EE this JSTL specification is decoupled, This makes the system more scalable and maintainable.

JSTL Custom Tags (iii)

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.