An in-depth study of tag library in JSP

Source: Internet
Author: User
Tags define empty expression flush hash variables version variable
Js

Tag Library taglib

tags are defined and distributed in a structure called a tag library, which is a collection of meta information and classes:
1. Label Processor: Java class that implements custom label functions.
2. Tag additional information (TEI): Provides a side series to the JSP container to confirm the label attributes and the class that created the variable.
3. Tag Library Descriptor (TLD): An XML document that describes the properties of a single label and the entire tag library.

Tag processor and tag additional information needs to be positioned where the JSP container class loader can be found. The tag library descriptor is available at the Fu location specified by the URL. The JSP1.1 specification requires that the JSP container accept a tag library of a jar file with a packaged genetic structure. The TLD must be a file named Taglib.tld in the/meta-inf directory, and the jar file will be copied to the/web-inf/lib directory.

first, the implementation of the label

1. Development steps
A. Define the label's name, attributes, declared variables, and the contents of the label body.
B. Write tag library descriptor TLD.
C. Write a label processor.
D. Use labels in JSP pages.

2.JSP page conversion steps in the JSP container:
There are three forms of JSP pages: JSP files, Java files, and class files.
A. Directive element <% @page%>, <% @include%> and <% @taglib%> provides conversion-time information to the JSP container.
The b.html line is converted to the Out.print () name in the _jspservice () method.
C. The statement of the script element is copied unaltered to the source code outside the _jspservice () method.
The expression of the script element is converted sequentially into the Out.print () in the _jspservice () method.
E. The scriptlet of the script element is copied unaltered to the _jspservice () method.
F. The behavior element is converted to Run-time logic code that performs its function.
G. Custom tags are extended to Java statements that invoke methods in their corresponding label processors.

3. Tags in the JSP container conversion steps:
The a.jsp container uses the taglib directive element to locate the tag library descriptor, matching the custom tags used in the page with the TLD.
B. Read the tag list of the tag library descriptor and the name of the class associated with each label.
C. When you encounter a label on a page, look for a tag library that is related to the label prefix with the specified name.
D. The container generates a series of Java statements that complete the label function using the tag structure information found in the TLD.


Second, Tag Library descriptor (TLD)

The tag library descriptor is an XML document that describes the entire tag library tag information and each label processor and its attributes in the library.

The DTD for the tag library descriptor consists of a simple element that contains some of the following child elements.
Entire Tag library tag information
Tlibversion the label library version number. is a dotted decimal number that consists of up to 4 decimal point-delimited digits.
The minimum version of the JSP specification required by the Jspversion tag library. such as JSP1.1
The abbreviated name of the shortname tag library. The JSP can use the name as the default prefix for the labels in the library.
The element of the URI tag Library unique URI. The typical URL location is from a downloadable taglib location.
Info Tag library description information.
Each label processor and its properties
Tag adds a label to the TLD that describes each label that makes up the library.
Name of the label used with the name prefix of the tag library, is the unique label identifier for the JSP container.
Tagclass the full name of the label processor class that implements the label.
The full name of the Teiclass label additional information (TEI) class. The Tei class gives information about the label processor creation variable and any validation of the label Division execution.
Bodycontent describes how the label processor uses the contents of the label body. There are three kinds of values:
Empty: Indicates that the label body must be empty;
JSP: Indicates that script elements are evaluated as well as templates and other tags.
Tagdependent: Body content is written to bodycontent, other script elements appear in the source form, and not be interpreted by the JSP container.
Human-readable descriptive information for the info label.
Attribute information that is encoded when a label is used. The property used to define the label.
Property name: The name of the property.
True|false: Whether the property will be encoded at the location used by the label.
True|false: Whether a property value can be specified with an expression.

third, label processor

A label processor is a Java class that performs custom label behavior by implementing a series of predefined methods called by the JSP container.
The label processor implements the label behavior, and the label processor is a Java class.

1. How the label processor works
A. Import javax.servlet.jsp and Javax.servlet.jsp.tagext packages.
B. Implement the tag interface or Bodytag interface in the Javax.servlet.jsp.tagext package. Bodytag is the tag's sub-interface.
C. Inherit TagSupport class or Bodytagsuppoert class. They are the default implementations of the above interfaces.
D. Overload public int doStartTag () throws Jspexception method.

2. Interface and implementation of label processor
Javax.servlet.jsp.tagext.Tag is the most basic interface for implementing labels.
Javax.servlet.jsp.tagext.TagSupport is the specific class that implements the tag interface.
It is usually useful to inherit the TagSupport class without directly implementing the tag interface. In addition to providing a default implementation for all required methods, the PageContext object and support for nested tags are saved.
The tag interface contains 4 constants representing the possible return codes for the doStartTag () and Doendtag () methods.
Eval_body_include when doStartTag () returns, indicate that the servlet should evaluate the label body.
Skip_body when doStartTag () returns, indicate that the servlet should ignore the label body.
Eval_page when Doendtag () returns, the rest of the page is evaluated.
Skip_page when Doendtag () returns, the rest of the page is skipped.
The method of the Tag interface
The servlet generated by the public void Setpagecontext (PageContext ctx) calls this method first before requesting the processor to perform other tasks, and the implementation class should save the context object so that it can be used during the label life. The label processor can access all JSP suppressed objects from the page context.
The public void SetParent (tag p) uses a single tag to find the label above it in the Operation Stack. Called immediately after the Setpagecontext.
Public tag GetParent () returns the parent tag.
The public int doStartTag () throws Jsp is invoked after the attributes encoded in the page context, parent tag, and start tag are set. The return code indicates whether the JSP implements the servlet to evaluate the label body.
public int Doendtag () throws jspexception called when a tag is encountered. The return code indicates whether the JSP is on the remainder of the New button page.
public void release () ensures that the page is called before it exits. Release the resource and reset the label processor state.
Methods of TagSupport Classes
The public static tag Financestorwithclass (Tag Thistag, Class CLS) finds the run-time tag stack for the parent tag processor that is required. A label processor can provide a method for calling the scope of a tag.
The public void SetId (String ID) saves and retrieves the name specified in the id attribute.
The public void SetValue (String name, Object o) Sets the value of the specified name in the local hash table.
The public Object getValue (String name) Gets the value of the specified name from the local hash table.
The public void RemoveValue (String name) deletes the value of the specified name from the local hash table.
Public enumeration GetValues () returns an enumeration of the keywords in the hash table.

3. Life cycle of label processor
A. Generating a servlet requires creating an instance of the label processor class. Implementation is usually a method of invoking the factory class of the JSP container, which contains a label processor instance pool so that it can reuse objects that are no longer in the active state.
B. Initialize the label processor to make the servlet aware of its existence. The servlet implements this process by calling two methods of the label Processor: Setpagecontext (PageContext ctx) and setparent (tag parent).
C. If the label has attributes, the value of the property is passed in to the object through the processor-supplied setter method. The property setter method is the only method required for a tag to support a property.
D. The context and parent tags of the page have been reset and have properties. The doStartTag () method of the label processor is invoked at this time to read the variables and perform the calculations and operations required to implement the labeling function. The doStartTag () method must return an integer number. Returns eval_body_include the label body is normally processed, and returns skip_body from the initial JSP page until the contents of the tag end tag are ignored.
E. When the label body is evaluated or ignored, the Doendtag () method that invokes the label processor is returned eval_page the remainder of the page is evaluated and the servlet code returns skip_page immediately from _jspservice ().

4. Interface and implementation of body label processor
Javax.servlet.jsp.tagext.BodyTag is the tag's sub-interface.
Javax.servlet.jsp.tagext.BodyTagSupport is the implementation of the Bodytag class.
Bodycontent is a subclass of Javax.servlet.jsp.JspWriter, but differs from its parent class.
The contents of the Bodycontent object are not automatically written into the servlet's output stream, but are accumulated in a string cache. When the label body is finished, its object can still be applied in the Doendtag () method, operated by the GetString () or Getreader () method. and modify and write the restored JspWriter output stream if necessary.
Methods of Bodycontent Classes
The public void Flush () throws IOException the replication Jspwrite.flush () method so that it always produces an overflow. The refresh write is invalidated because it is not connected to the actual output stream that will be written.
The public void Clearbody () resets the bodycontent cache to null.
Public reader Getreader () returns the reader body content.
public string getString () returns a string containing the contents of the body.
The public void Writeout (write W) writes the body content to the specified output.
Public Jspwrite getenclosing Write () returns the next higher writer object in the stack (possibly another Bodycontent object).
The Bodytag interface defines a new integer constant
Eval_body_tag when doStartTag () returns, the new Bodycontent object is created and associated with this label processor. When Doafterbody () returns, the JSP servlet evaluates the body again after modifying any variable that the label controls.
The method of Bodytag interface
public void Setbodycontern (bodycontent out) has been written to the current JspWriter, and a new bodycontent is invoked by the JSP servlet after it is created, after doStartTag ().
The life period method that is called before the body is evaluated after the public void Doinitbody () throws Jspexception setbodycontent (). If the body is evaluated more than once, this method is called only once.
The life period method that is invoked when the public init doafterbody () throws jspexception is evaluated, and the Bodycontent writer is still active. This method must return Eval_body_tag or skip_body, and the body is evaluated again if the Eval_body_tag is returned.
Methods of Bodytagsupport Classes
public int doStartTag () throws jspexception the doStartTag () method in the replication TagSupport.
The public int Doendtag () throws Jspexception invokes the Doendtag () method in TagSupport to return the result.
The public void Setbodycontent (bodycontent out) saves the new body content object in a protected member variable bodycontent, which the subclass can access directly.
public void Doinitbody () throws jspexception defaults to nothing. is overwritten by subclasses that need to perform initialization.
The public int doafterbody () throws Jspexception is called by the JSP servlet each time the body is evaluated, and the body-like object is still active. Return to Skep_body or eval_body_tag the body is evaluated again
public void Release () sets the Bodycontent object to null and then calls Super.release ().
Public bodycontent getbodycontent () returns the Bodycontent variable. A subclass can already access a protected variable, but this method allows an unrelated tag-handling class to send output to this body content.
Public JspWriter Getpreviousout () An easy way to call Getenclosingwriter () on bodycontent variables and return results.

5. Life cycle of Body label processor

A. Generating a servlet requires creating an instance of the label processor class. Implementation is usually a method of invoking the factory class of the JSP container, which contains a label processor instance pool so that it can reuse objects that are no longer in the active state.
B. Initialize the label processor to make the servlet aware of its existence. The servlet implements this process by calling two methods of the label Processor: Setpagecontext (PageContext ctx) and setparent (tag parent).
C. If the label has attributes, the value of the property is passed in to the object through the processor-supplied setter method. The property setter method is the only method required for a tag to support a property.
D. The context and parent tags of the page have been reset and have properties. Invokes the doStartTag () method of the label processor, which can read these variables and perform the calculations and operations required to implement the labeling function.
The doStartTag () method must return an integer number.
Return to eval_body_tag the normal processing of the label body (jump to E);
Returns skip_body from the initial JSP page until the contents of the tag end tag are ignored. (Jump to f)
E. If Eval_body_tag is returned, the label body is processed normally.
E1. Saves the current JspWriter object in the stack, creates a new Bodycontent object, and saves it as an out object of the JSP page in the context-wide attribute named name. and call its setbodycontent () method.
E2. Invokes the Doinitbody () method for initialization.
E3. Handle the label body. Writes the output to the Bodycontent object, which relies on the label element of the TLD , there are three possible values.
E4. Call the Doafterbody () method to write the body contents to the JspWriter, which can be implemented as follows:
JspWriter Out=bodycontent.getenclosingwriter ();
Out.println (Bodycontent.getstring ());//bodycontent.writeout (out);
Bodycontent.clear ();
The E5.doafterbody () method returns two possible types:
When Eval_body_tag is returned, the label body is evaluated, which is typical for arrays and enumerations to be recycled.
When you return to Skip_page, continue the rest of the page.
E6. Body content is complete, so the process of creating it is reversed:
The Pagecontent.popbody () method is invoked to retrieve the preceding JspWriter object.
Sets the writer back to the out implied object.
F. The label body is evaluated or ignored to call the Doendtag () method, allowing the label processor to send back content like an output stream.
Return Eval_page the remainder of the page is evaluated;
Returns skip_page the servlet code immediately returns from _jspservice ().
G. The contents of this body are still available in the protected Bodycontent object.
It can be written to the servlet output stream:
JspWriter out=pagecontext.getout ();
Out.println (Bodycontent.getstring ());
Or
Bodycontent.writeout (Pagecontext.getout ());

6. Label additional Information class


Iv. Labeling Instructions

The purpose of the TAGLIB directive element is to specify the location of the TLD and set a short alias that distinguishes it from the label on the page.
Syntax: <%@ taglib prefix= "tag prefix" uri= "Tagliburi"%>
Properties: Prefix: A unique identification used to identify a tag library. URI: The URI of the tag library itself.
The URI does not have to point to an actual file, which is a unique identifier of the JSP container that can find the actual file location in Web.xml.



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.