Taglib principle and implementation (Author Walkingwithjava)

xiaoxiao2021-03-06  14

What is TAG? How to implement a TAG?

A tag is a normal Java class, which is the only special thing that it must inherit the Tagsupport or the Bodytagsupport class. These two classes provide some ways to be responsible for the interaction between JSP pages and the classes you have written, such as input, and output. And these two classes are provided by the JSP container without developers themselves. In other words, you only need to inherit tagsupport or Bodytagsupport, and do some special work, your class is a tag. And it is responsible for interacting with the JSP page, don't worry about you.

"Special Work" usually has the following steps:

1) Provide a SET method for the attribute, and this property can be set at the JSP page. Take the JSTL tag as an example C: Out value = "" /, this value is the entry between JSP data to TAG. So there must be a setValue method in TAG, and the specific properties can be called Value. For example setValue (string data) {this.data = data;}

This "value" name is defined in TLD. What is the name, you can provide a corresponding set method in tag.

2) Handle DostartTAG or DOENDTAG. These two methods are Tagsupport provided. Or C: OUT VALUE = "" / Take the case, when the JSP parsing this tag, the DOSTAG event is triggered at "<", ">" triggers the DOENDTAG event. Logical operations are usually performed in DostartTag, and the output is controlled in DOENDTAG.

3) Write a TLD file.

4) Import TLD on the JSP page. This way, your JSP page can use your own TAG.

Usually you will find that most of your activities are concentrated in the DostartTAG or DoreTAG method. In this way, after familiar with some interfaces and classes, it is easy to write Taglib. As the author of JSP Design: The logic inside is slightly more complicated, but after all, there is no rocket that is so difficult.

A simple example: Outputtag

Package diegoyun;

Import javax.servlet.jsp.jspexception;

Import javax.servlet.jsp.jspwriter;

Import javax.servlet.jsp.tageXt.tagsupport;

/ **

* @Author chenys

* /

Public Class Outputtag Extends Tagsupport

{

Private string name = NULL;

Public void setname (String name)

{

THIS.NAME = Name;

}

Public int desartTartTag () throws jspexception {

Try

{

Jspwriter out = pageContext.getut ();

Out.print ("Hello!" Name;

}

Catch (Exception E)

{

Throw new JSPEXCEPTION (E);

}

Return Eval_page;

}

}

brief introduction:

1 How to output it to the JSP page: call jspwriter jspwriter out = pageContext.get (); out.print ... Remember this method.

2 How to proceed after output, the function will return one of several values. Eval_page indicates that the TAG has been processed and returned to the JSP page. There are several values, such as Eval_Body_Again and Eval_Body_include, etc., follow we will discuss. Write TLD

DOCTYPE TAGLIB

PUBLIC "- // Sun microsystems, Inc.//dtd JSP Tag Library 1.2 // en"

"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

1.0

1.2

Diego

OUT

Diegoyun.outputtag

EMPTY

Name

false

FALSE

Add a new TLDS folder under Web-INF, name Diego.TLD, put it under the TLDS folder. The path should be like this: Web-inf / TLDS / DIEGO.TLD

Brief description of TLD:

Short-name: The name of Taglib is also called a prefix. For example, "c: out value =" "/"

Name: Tag's name. For example, "OUT" in "c: out value =" "/", our class is also named OUT, because there is a prefix to distinguish, it will not be confused

TAG-Class: Specific TAG class. Band name

Body-content: refers to the content between tags. For example, C: out value = "" ... / c is Body-Content between the start and the closing tag. Since there is no Body-Content, set it to EMPTY

"Attribute" Name: Attribute Name. For example, Value in C: Out value = "". The name can be arbitrarily, as long as the class provides the corresponding set method.

Required: Whether it will be required.

RTEXPRVALUE: Whether to support runtime expressions. This is the powerful function of TAG. We will discuss later. Temporary set to false

Write a JSP page

<% @ page language = "java"%>

<% @ Taglib Uri = "/ Web-INF / TLDS / DIEGO.TLD" prefix = "DIEGO"%>

Test TAG:

My programming environment is Eclipse Tomcat. Start the server, if everything follows the steps above, you can see the Test Tag: Hello! Diegoyun word. The easiest tag is coming out. It's not difficult, is it? (T111)

转载请注明原文地址:https://www.9cbs.com/read-49521.html

New Post(0)