JSP Custom Label Basics Learning _jsp Programming

Source: Internet
Author: User
Tags tld

In actual development, such as to simplify the presence of a large number of JSP scripts in the 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 development needs. such as: paging. So you need to learn how to customize your own tag library.

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

Writing label processing classes
Need to inherit or implement related classes or interfaces

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

Introduce tags in the JSP and use the
Use the taglib instruction to introduce a tag library, which is then used.

Self-defined Label class system

Learn more about the next few classes and interfaces:

---| Jsptag interface

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

----| Tag interface

The interface mainly describes the generality 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. and provide lifecycle methods such as: Methods that are executed automatically at the beginning of the label and at the end of the connection.

------| TagSupport class

Primarily responsible for handling the properties of labels.

-------| Bodytagsupport class

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

Experience

1. Processing class

public class Hellohanler implements Tag {
  private pagecontext pagecontext = null;
  Execute public int Doendtag () at the end of the tag
  throws jspexception {return
    0;
  }
  When the label starts, execute public
  int doStartTag () throws Jspexception {
    //To output a hello message to the page
    jspwriter out = Pagecontext.getout ();
    Output information
    String info = "Hello custom tag";
    try {
      Out.write (info);
    } catch (IOException e) {
      e.printstacktrace ();
    }
    return 0;
  }
  Gets its parent tag public
  tag GetParent () {return
    null;
  }
  Free public
  void release () {

  }
  //Set JSP context object public
  void Setpagecontext (PageContext pc) {
    This.pagecontext = PC;
  }
  Set parent tag public
  void SetParent (Tag t) {

  }
}

2. Description document

<?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/ Java ee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd "
  version=" 2.1 ">
 <!--2. Write tag library description file--> 
 <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. Introducing

<% @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 label handling classes that can handle tag properties

public class Showdate extends TagSupport {
  //To facilitate the acquisition of attributes, you can directly treat a property variable with the same name as a property in a class and provide a get and set method
  private String pattern;
  Public String Getpattern () {return pattern
    ;
  }
  public void Setpattern (String pattern) {
    This.pattern = pattern;
  }

  The label starts with the public
  int doStartTag () throws Jspexception {
    //Creation Date Object Date
    = new Date ();
    Create a formatted object
    simpledateformat format = new SimpleDateFormat (Getpattern ());
    Format
    String str = format.format (date);
    Gets the JSP context object
    PageContext PageContext = this.pagecontext;
    Gets the out output stream of the JSP
    jspwriter out = Pagecontext.getout ();
    Output
    try {
      out.write (str);
    } catch (IOException e) {
      e.printstacktrace ();
    }
    return Super.dostarttag ();
  }


2. Description document

 <taglib Tag library description file root element xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "H Ttp://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 tag library description file--> <tlib-version>1.0</tlib-version> Specify the version of the tag library (must) <SHORT-NAME>JNB</SHORT-NAME&G      T  Specifies the short name of the tag library (must) <tag> specify a label to start <name>showdate</name> label <tag-clas S>cn.itcast.custom.showdate</tag-class> Specifies the label processing class <body-content>empty</body-content> the specified label body. JSP (has) empty (NO) <attribute> description attribute <name>pattern</name> property name <required Description information for the >true</required> property <rtexprvalue>true</rtexprvalue> property value </attribute> & lt;/tag> </taglib> 

3. Introduction and use

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

To implement a custom label with a label body

1. Label Processing class

 public class Showdatebybody extends Bodytagsupport {//To facilitate getting properties, define property variables with the same name as the property directly in the class
  You can provide the get and set methods private String pattern;
  Public String Getpattern () {return pattern;
  public void Setpattern (String pattern) {This.pattern = pattern;
    Automatically execute public int dostarttag () () throws Jspexception {///create Date Object dated = New Day () when the label starts
    Create a formatted object SimpleDateFormat format = new SimpleDateFormat (Getpattern ());
    Format String str = format.format (date);
    Gets the JSP context object PageContext PageContext = This.pagecontext;
    Gets the out output stream of the JSP jspwriter out = Pagecontext.getout ();
    Get the contents of the label bodycontent 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 ();
  return Super.dostarttag (); }
}

2. Description document

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

The above is the entire content of this article, I hope to help you learn.

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.