Custom label class, custom label

Source: Internet
Author: User
Tags tld

Custom label class, custom label

Tags can be used to replace the scripts on the jsp page to make the page more concise, easier to maintain, and increase program security.

The Java EE api has an interface javax. servlet. jsp. tagext. JspTag, which is the root interface of all labels.

After jsp2.0, a simpler javax. servlet. jsp. tagext. SimpleTag is defined. This interface describes how to customize tags.

Label classification:

  • Traditional labels
  • Simple tag SimpleTag

SimpleTag System

SimpleTag has an implementation class SimpleTagSupport, which is generally inherited by the custom label class.

To define a tag:

SimpleTag interface method description

  • The function of the doTag method: Describes the tag function (automatically called)
  • SetJspContext: its parameter is JspContext, which is used to pass the pageContext object of the page to the label class. (Very useful for obtaining other eight objects)
  • SetJspBody: cache the content of the label body to the memory object, and the JspFragment parameter is equivalent to the content of the label body.
  • GetParent setParent introduces the tag's parent tag. (Not required)

 

The following is a custom label class: (a java class that prints the content of the label body multiple times)
Package online. mytag; import java. io. IOException; import javax. servlet. jsp. jspException; import javax. servlet. jsp. jspWriter; import javax. servlet. jsp. tagext. simpleTagSupport; public class PrintTag extends SimpleTagSupport {private int count; // the field here must be in. if you use the tld file, you must provide the get/set Method public int getCount () {return count;} public void setCount (int count) {this. count = count;} // print @ Override public void doTag () throws JspException, IOException {String str = getJspBody (). toString (); JspWriter out = getJspContext (). getOut (); for (int I = 0; I <count; I ++) {out. write (str );}}}
The following is a custom tag class: (used to obtain the ip address of the browser)
Package online. mytag; import java. io. IOException; import javax. servlet. jsp. jspException; import javax. servlet. jsp. pageContext; import javax. servlet. jsp. tagext. simpleTagSupport; public class IpTag extends SimpleTagSupport {/*** output the browser's ip address to the browser */@ Override public void doTag () throws JspException, IOException {String ip = (String) (PageContext) (getJspContext ())). getRequest (). getRemoteAddr (); getJspContext (). getOut (). write (ip );}}
The. tld file corresponding to the above two tag implementation classes:
<? 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"> <short-name> my </short-name> // here is the refer attribute value of taglib when this label is used:<% @ Taglib uri = "online. mytag. IpTag" prefix = "my" %><Uri> http://java.sun.com/jsp/jstl/mytag </uri> // here is the uri attribute of taglib when this label is used <tag> // every <tag> here specifies a label java class <description> output the ip address of the browser </description> // some descriptions, that is, the <name> getIp </name> // tag name <tag-class> online when the label class is used. mytag. ipTag </tag-class> // The full class Name of the tag class <body-content> empty </body-content> // This attribute indicates whether the tag body exists. </tag> <tag> <description> output the tag body cyclically Based on the tag property value </description> <name> print </name> <tag-class> online. mytag. printTag </tag-class> <attribute> // refers to the fields in the label class, which have several fields, there are several attribute tag <description> cycles </description> <name> count </name> // when this tag is used, attribute name <required> true </required> // specifies whether the attribute is required <rtexprvalue> true </rtexprvalue> // indicates whether the attribute supports el expressions </attribute> </tag> </taglib>
Use custom tags on the jsp page:
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@taglib uri="online.mytag.IpTag" prefix="my"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">Draw a graph to show the sub-tags under the tag: (Note: The attribute field must provide the corresponding get/set Method in the tag class)

 

To sum up custom tags:

We use simple tags, that is, implementing SimpleTag or inheriting SimpleTagSupport to implement custom tags.

Basic steps:

1. Create a label class implements SimpleTag extends SimpleTagSupport class

  • JspContext indicates the pageContext object on the jsp page.
  • JspFragment indicates the content in the label body. Through its invoke method, the content of the label body can be specified to the output stream output of the invoke method parameter. If it is null, the default is the out object associated with the current jsp page.

2. Create a tld file in the WEB-INF/

  • Use <tag> to describe tags
  • Use <name> under <tag> to define the tag name
  • Use <tag-class> under <tag> to define the tag class
  • Use <body-content> under <tag> to describe whether there is content in the tag body. Two values can be used:
  • Empty scriptless
  • Use <attribute> under <tag> to describe the attribute, but the corresponding get/set method must be provided in the label class.
  • Use <name> under <attribute> to declare the attribute name
  • Use <required> under <attribute> to declare whether the attribute must exist.
  • Use <rtexprvalue> under <attribute> to declare whether attribute can use el expressions

Note:

  • When creating a tld file, you must select a version later than 2.0 and have defects after creating the tld file. You must manually copy the first value in schemaLocation to the xsd file.

(Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd ")

3. Use taglib to import tags on the jsp page

  • To use tags, you must use the taglib pilot import label.

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.