Basic knowledge of JSP custom tags and basic knowledge of jsp custom tags

Source: Internet
Author: User
Tags tld

Basic knowledge of JSP custom tags and basic knowledge of jsp custom tags

In actual development, for example, to simplify JSP scripts, we need to use the standard tag library and EL expressions, but the labels provided in the new tag library are limited, it is impossible to fully meet the development needs. For example, pagination. Therefore, you need to learn how to customize your own tag library.

To implement custom tags, You need to perform the following steps:

Tag Processing
Classes or interfaces to be inherited or implemented

Compile the label description file
The file is an XML file and must be placed in the WEB-INF directory of the website

Introduce tags in JSP and use
Use the taglib command to introduce the tag library and then use it.

Custom tag Class System

For more information, see the following classes and interfaces:

--- | JspTag Interface

This interface is a typical tag interface. The class that implements this interface can process tags. Seralizable

---- | Tag Interface

This interface mainly describes the common characteristics of the tag processing class, but the class implementing this interface cannot process the TAG body. This interface defines the communication protocol between the tag processing class and the JSP page. In addition, the lifecycle method is provided, for example, the method automatically executed when the tag starts and ends.

------ | TagSupport class

It is mainly responsible for processing tag attributes.

------- | BodyTagSupport class

This class mainly processes the label bodies of tags.

Experience

1. Processing class

Public class HelloHanler implements Tag {private PageContext pageContext = null; // when the Tag ends, execute public int doEndTag () throws JspException {return 0 ;} // execute public int doStartTag () throws JspException when the tag starts {// Output A hello Message to the page. JspWriter out = pageContext. getOut (); // output String info = "hello custom tag"; try {out. write (info);} catch (IOException e) {e. printStackTrace ();} return 0;} // obtain its parent Tag public Tag getParent () {return null;} // release public void release () {}// set the jsp context object public void setPageContext (PageContext pc) {this. pageContext = pc;} // set the parent Tag public void setParent (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. compile the 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. Introduction

<%@taglib uri="/WEB-INF/test.tld" prefix="jnb"%>   <br/><jnb:hello/>

JSP1.2 for custom tag Development

Customize a tag for a real date.

1. Implement tag processing class that can process tag attributes

Public class ShowDate extends TagSupport {// to facilitate the acquisition of attributes, you can define the attribute variables with the same name as the attributes in the processing class and 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 when the tag starts {// create Date object date = new Date (); // create the formatting object SimpleDateFormat format = new SimpleDateFormat (getPattern (); // format String str = format. format (date); // obtain the JSP context object PageContext pageContext = this. pageContext; // get the OUT output stream of JSP JspWriter out = pageContext. getOut (); // output try {out. write (str);} catch (IOException e) {e. printStackTrace ();} return super. doStartTag ();}}

2. description file

<Taglib libraries label 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. compile the tag library description file --> <tlib-version> 1.0 </tlib-version> required to specify the version of the tag library) <short-name> jnb </short-name> tags specifies the short name (required) of the tag library) <tag> begin specifies a tag to start <name> showdate </name>. The tag name <tag-class> cn. itcast. custom. showDate </tag-class> labels specifies the tag processing class <body-content> empty </body-content> labels specifies the tag body, JSP (with) empty () <attribute> condition description attribute <name> pattern </name> condition attribute name <required> true </required> description of the condition attribute <rtexprvalue> true </rtexprvalue> condition attribute value </attribute> </tag> </taglib>

3. Introduction and use

<% @ Taglib uri = "/WEB-INF/date. tld" prefix = "date" %> <date: showdate pattern = "yyyy MM dd a E"/>

Implement custom tags with tags

1. Tag Processing

Public class ShowDateByBody extends BodyTagSupport {// to facilitate the acquisition of attributes, you can define the attribute variables with the same name as the attributes in the processing class and 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 when the tag starts {// create Date object date = new Date (); // create the formatting object SimpleDateFormat format = new SimpleDateFormat (getPattern (); // format String str = format. format (date); // obtain the JSP context object PageContext pageContext = this. pageContext; // get the OUT output stream of JSP JspWriter out = pageContext. getOut (); // get the 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 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>

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Use custom labels, XML, and XSL to display data in asp.net (C #).
  • Jsp custom tag instance
  • Differences between using custom tags in IE Firefox
  • Summary of the implementation process of JSP custom tag Taglib
  • Jsp custom tag Technology (implementation principles, code, and platform building steps)
  • How to obtain user IP addresses using JSP custom tags
  • ForEach traversal and escape character examples of simple custom jsp labels
  • How to use custom tags in the ThinkPHP Template
  • Custom tag library instances in the ThinkPHP Group
  • Examples of JSP custom tag rtexprvalue attribute usage

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.