Develop JSP custom behavior

zhaozj2021-02-16  51

Develop JSP custom behavior

1. To develop a custom behavior, you need to use a series of classes and interfaces, which are called tag extension mechanism in the JSP1.1 specification. All interfaces and classes that implement the markup handler are defined in the javax.servlet.jsp.tagext package. Two major interfaces are TAG and Bodytag, respectively. In order to make the development tag handler easier, the API defines two supported classes: tagsupport and bodytagsupport. These two classes provide the default implementation for both interfaces above.

2. The tag library is a collection of custom behavior. In addition to the class file of the tag handler in the tag library, the tag library must also include a TLD file. This is an XML file that maps all the names of custom behavior to the corresponding tag handler class, and describes all attributes supported by each custom behavior. Class files and TLDs can be packaged into a JAR file to facilitate installation.

3. Development, configuration, and use of a simple custom behavior usually need to do these parts.

(1) Implement a marker processing program class. Compile this class, then put the generated class files in the application's web-inf / classs directory.

(2) Create a TLD file. Look at the simple instance below.

DOCTYPE TAGLIB

Public "- // Sun microsystems, inc .//dtd jsp tag library 1.1 // en"

http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd>

1.0

1.1

Test

Hello

com.mycompany.hellotag

Empty

Name

Take this file name * .tld and put it in the application's web-inf / TLDS directory.

(3) Now you can use custom behavior in the JSP page.

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

When this page is requested, the JSP container uses this TLD to discover classes corresponding to custom behavior. Then it will call all the corresponding methods to join the corresponding text into the response. The above is all things we have to do with the simplest case.

4. Let's take a look at the most important TAG interface:

Public void setPageContext (PageContext PageContext);

Public Int DostartTag () THROWS JSPEXCEPTION; PUBLIC INT DOENDTAG () THROWS JSPEXCEPTION

Then we understand the implementation of these methods provided by the Tagsupport class.

Public Class Tagsupport implements tag, serializable {

Protected pageContext pageContext;

.......

Public void setPageContext (PageContext pageContext) {

THIS.PAGECONTEXT = PageContext;

} // This method is called by the JSP container before using the tag handler.

Public int desartTartTag () throws jspexception {

Return Skip_body;

} // When you encounter the start flag, the JSP container will call the dostartTag () method.

PUBLIC INT DOENDTAG () THROWS JSPEXCEPTION {

Return Eval_page;

} // When the end flag is encountered, the JSP container will call the DOENDTAG () method.

}

5. Developing a marker handler that does not need to do anything to do anything is very simple. However, some methods are required for a marker handler that requires a behavior. These methods are defined by the Bodytag interface, which extends the TAG interface.

6. A process of executing a tag handler with a behavior.

(1) SetAttr1 ("Value1")

(2) SetAttr2 ("Value2")

(3) dostarttag ()

(4) setBodyContent ()

(5) DOINITBODY ()

(6) DOAFTERBODY ()

(7) doendtag ()

7. The Bodytagsupport class has many practical methods to handle behavior.

Such as: getBodyContent (), getPreviousOutout ()

8. How to make multiple behaviors to collaborate with each other.

How is the inner behavior telling the outer behavior to define the parameter information? The answer to this question is in some TAG interface methods implemented by the Tagsupport class and one tool method.

These TAG interface methods are setParent () and getParent (). A nested tag handler typically has a reference to the parent element accommodated. Therefore, a tag handler at any nested level can use a getParent () method to get its parent element, then you can get the parent element of the parent element. You can go up until it reaches a tag without a parent element (ie getParent () returns NULL). This means that the outermost layer has been reached.

Usually, a marker handler is only interested in the parent element that it is to work with. So, a good idea is to develop a method, which will continue to trace the hierarchy until the parent element of the tag handler is interested. And this is the work made by the FindanceStorwithClass () method implemented by the Tagsupport class.

Public Static Final Tag FindanceStorwithClass (Tag from, Class Klass) {

Boolean isinterface = false;

IF (from == null || klass == null || (! Tag.class.IsassignableFrom (klass) &&! (Isinterface = klass.isinterface ())) {

Return NULL;

} // If from or klass is null, or klass is a class and does not implement a TAG interface, it is clear that the current TAG can't find the parent element of the KLASS class.

For (;;) {

Tag tag = from.getParent ();

IF (tag == null) {

Return NULL;

} // Indicates no parent elements

IF (Isinterface && Klass.Isinstance (Tag) || Klass.IsassignableFrom (tag.getclass ()))))

Return tag; // If Klass is an interface and current TAG implements the interface, then found.

// or the current tag is a klass type, then found it.

Else

From = tag;

}

}

9. Create a new variable through behavior

When custom behavior creates a variable, it must work with the JSP container.

For a tag handler, create a new object and make other behaviors and JSP script code access to this object, need two things:

(1) The JSP container must know the name of the object and the Java type, so that it can generate a code for the variable declaration.

(2) The variable must be placed in a JSP scope so that it can be found via the FindAttribute () method and can assign this variable.

The first thing is done by a class named TAGEXTRAINFO. When you develop tag handles for a behavior of an object, you must create a subclass of the TAGEXTRAINFO class. The JSP container needs such an instance of such a class when generating code.

10. Development iteration

When developing iterative behavior, there is usually a TAGEXTRAINFO subclass that can access the loopID variable during the loop.

11. How to create TLD

When the JSP container converts custom behavior elements into code created and called the correct tag handler, the JSP container also requires information about which tag handler implements which custom behavior element. This information is obtained from TLD. In addition, the JSP container also uses TLD information to verify that the attribute list of behavioral elements is correct.

TLD is an XML file that contains information about all custom behavior in a library. The JSP page with custom behavior must be identified to identify the corresponding TLD, and it is also possible to identify the namespace prefix for behavior in the page with the Taglib command element. Such as:

<% @ Taglib URI = "/ Web-INF / TLDS / ORATAGLIB_1_0.TLD" prefix = "ORA"%>

.

When you encounter a prefix matching behavior in the JSP page, the JSP page will use TLD to find the information it needs.

The contents of the TLD file:

(1) The top part: the standard XML declaration and a DOCTYPE statement. (These two declarations specify the document type definition of the file Document Type Definition, DTD).

(2) element: The main element of TLD, which contains all specific elements of the depicted library.

(3) Element: Specifies the version of the tag library. Must (4) element: Specifies the version of the JSP specification dependent on the library. The default is 1.1. Optional

(5) Element: Elements must be elements. The value of this element cannot contain spaces or other special characters, nor cannot begin with numbers or underscores.

(6) Elements: Optional, character rules are the same.

(7) element: optional,

(8) At least one element must also be included in the TLD. elements include other elements of all aspects of custom behavior:

l element is necessary. It contains the name corresponding to the custom behavior element, this name is unique.

l elements are also necessary.

l If the behavior introduces the variable, or you need to verify syntax, then use the element. This element is optional to develop a full name of the TAGEXTRAINFO subclass.

l element is an optional element, which may be: Empty, JSP, or TagDepend.

l elements are optional to describe the purpose of behavior.

l TAG elements must also set elements for each property of the behavior. Of course, each element also contains another element of the description of the properties: , , and

12. Syntax verification

(1) The simplest syntax verification is:

When the JSP container is converted into a servlet, it compares the specification of the custom behavior element and the behavioral elements in TLD. This allows you to verify the errors on the syntax.

(2) Certain syntax verification is much more complicated. Such as: Some properties depend on whether other properties appear. Some attributes are row his, if this is used, you can't use that. An optional properties must be used with other optional properties. To verify these dependencies, the JSP container must seek support to the TAGEXTRAINFO subclass of the tag handler.

After the JSP container checks everything it can check, it will look for the TAGEXTRAINFO subclass (defined by the element) for behavior. If the TAGEXTRAINFO subclass is defined, it places all attribute information in an instance of the TagData class, and calls TAGEXTRAINFO's isvalid () method, the parameter is a TagData class with information.

13. How to reuse tag handlocks

It is recommended to complete the resetting all properties in the Release () method.

14. How to pack and install the tag library

After completing the development, you may want to pack all the tag handler classes, TAGEXTRAINFO classes, and tagged BEAN and TLDs in a JAR file. This makes it easier to install the library in the application. TLD must be stored in /meta-inf/taglib.tld in the JAR file. (1) In order to create a JAR file, arrange the files in the directory according to the following structure.

Under Meta-Inf: taglib.tld

Place all classes on a COM directory

File Structure Settings Use the JAR command to create a JAR file:

JAR CVF Maojb.jar Meta-Inf COM

(2) Copy the JAR file to the web-inf / lib directory of the application.

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

New Post(0)