Tuesday, August 3, 2010

Tag Library JSP Servlet call



Taglib Tag library

Tags are defined and distributed in a structure known as the tag library, a tag library is composed of meta-information and a collection of classes:

1. Tags processors: implementation of the Java class custom labels.

2. Label Additional Information (TEI): JSP container to provide edge series to confirm the tag attributes and create a variable class.

3. Tag library descriptor (TLD): description of a single tag and the tag library attributes of XML documents.

Tag handler and tag additional information needs to be positioned in the JSP container class loader can find it. Tag library descriptor can be specified in the URL location of the symbol means. JSP1.1 specifications to accept a package JSP container will cause the structure of the tag library JAR file. TLD must be / META-INF directory of the file named taglib.tld, JAR files are copied to the / WEB-INF / lib directory.

1, the label achieved

1. Development steps

a. the definition of tag names, attributes, declarations of variables and labels body content.

b. write tag library descriptor TLD.

c. write tag handler.

d. the use of labels in the JSP page.

2.JSP page in the JSP container conversion steps:

JSP page there are three forms: JSP file, java file and class files.

a. command element, and the JSP container to provide conversion information.

b.HTML line _JSPService () method in accordance with the order to switch to out.print () language name.

c. statement of the script element is copied intact to the _JSPService () method outside of the source code.

d. script element expression in _JSPService () method in accordance with the order to switch to out.print () language name.

e. script elements Scriptlet be copied intact to the _JSPService () method.

f. conduct element is converted into run-time performance of its functions the logic code.

g. custom tag is expanded to its corresponding tag handler calls the method in the Java statement.

3. Tags in the JSP container conversion steps:

a.JSP container positioning using taglib directive element tag library descriptors, used to customize the page to match labels and TLD.

b. Read the label tag library descriptor list and the name of each class of associated label.

c. encounters a tag in the page, the look and name tags with the specified prefix related to a tag library.

d. Containers used in the TLD to find the tag structure information to generate a complete Java statement labeling function.

2, tag library descriptor (TLD)

Tag library descriptor is a description of the tag library tag information and library in each tag and its attributes XML document processor.

The DTD tag library descriptor element by a simple composition, this element contains the following number of child elements.

The tag library tag information

鈼?tlibversion tag library version number. Is a point decimal number, up to 4 groups of numbers separated by decimal points.

鈼?JSPversion JSP tag library specification required minimum version. For example JSP1.1

鈼?shortname acronym name tag library. JSP can use the tag name as the default library prefix.

鈼?uri URI tag library the only element. Can be downloaded from the URL location of typical taglib location.

鈼?info Tag library descriptions.

Each tag and attribute tag in the TLD processor to add tags, description tags for each component library. name and the name of the prefix tag library for use with the label's name, is the only label JSP container identification. tagclass label tag processor class to achieve the full name. teiclass tag additional information (TEI) class full name. TEI class is given on the label processor to create variables and on the label of the Executive Secretary of any validation information. bodycontent describes how to use the label tag handler body content. There are three values:

鈼?empty: said tag body must be empty;

鈼?JSP: that script elements and templates, and other labels as being assessed.

鈼?tagdependent: body content is intact written BodyContent, other script elements in source code form, without being interpreted JSP container.

info label human-readable descriptive information. attribute is encoded using the label attribute information. Used to define the label attribute. Property name: property name.

鈼?true | false: the location of property used in the label is to be encoded.

鈼?true | false: the specified property value can use the expression.

Third, the label processor

JSP tag handler is a container through the realization of a series of pre-defined method called custom tag implementation of a Java class act.

Tag handler implements the behavior of tags, labels processor is Java class.

1. Label processor work

a. Import javax.Servlet.JSP and javax.Servlet.JSP.tagext package.

b. achieve javax.Servlet.JSP.tagext package Tag interface or BodyTag interfaces. BodyTag is sub-interfaces Tag.

c. inherited class or BodyTagSuppoert TagSupport class. They are the interface default implementation.

d. Heavy public int doStartTag () throws JSPException method.

2. Label processor interface and implementation

鈼?javax.Servlet.JSP.tagext.Tag is to achieve the most basic interface label.

鈼?javax.Servlet.JSP.tagext.TagSupport Tag interface to achieve the specific class.

Usually not directly implement class inheritance tagSupport Tag interfaces are often useful. In addition to all the necessary methods provide the default implementation, but also save the pageContext object and the nested tags support.

Tag interface contains four constants that doStartTag () and doEndTag () method may return code.

EVAL_BODY_INCLUDE When the doStartTag () returns, the specified Servlet tag body should be assessed.

SKIP_BODY When the doStartTag () returns, the specified Servlet tag body should be ignored.

EVAL_PAGE When the doEndTag () returns, the rest of the specified page should be assessed.

SKIP_PAGE When the doEndTag () returns, the rest of the specified page was skipped.

Tag interface methods

public void setPageContext (PageContext ctx) generated Servlet the request processor to perform other tasks before the first call to this method, the implementation class context object should be preserved so that it can use the life of the label. From the page in the context of JSP tag handler can access all the hidden objects.

public void setParent (Tag p) using a standard A stack can be found operating the label on it. Immediately after the setPageContext call.

public Tag getParent () returns the parent tag.

public int doStartTag () throws JSP page set the context, the parent tag and start tag after the coding of the property call. Return code shows whether the JSP Servlet implementation body to assess the label.

public int doEndTag () throws JSPException whether the mark when the encounter node call. JSP code that is returned to New York on the following pages of the rest.

public void release () to ensure that the page is called before exiting. Release of resources and reset the label processor state.

TagSupport class methods

public static Tag finAncestorWithClass (Tag thisTag, Class cls) is the parent tag handler to find the required run-time label stack. Processor can provide a label within the sub-label called method.

public void setId (String id) save and retrieve the id attribute in the specified name.

public void setValue (String name, Object o) in the local hash table setting specifies the name of the value.

public Object getValue (String name) from the local hash table value for the specified name.

public void removeValue (String name) Remove from the local hash table, the value of the specified name.

public Enumeration getValues () returns a hash table in the enumeration of keywords.

3. Label processor lifetime

a. generate Servlet need to create an instance of tag handler class. Implementations call the JSP container is usually a factory class method, factory class instance pool contains a tag processor so that it can reuse the object is no longer active.

b. Initialize label processor, Servlet informed of its presence. Servlet label processor by calling two methods to achieve this process: setPageContext (PageContext ctx) and setParent (Tag parent).

c. If the tag has attributes, attribute values passed through the processor to provide setter methods to the object. Property setter method is a label the only way to support the required attributes.

d. the page context and parent label has been transferred home and already have property. At this point called label processor doStartTag () method, which can be read and implemented to achieve these variables needed to calculate standard A function and operation. doStartTag () method must return an integer. Back EVAL_BODY_INCLUDE the normal processing tag body, to return SKIP_BODY from the initial JSP page until the end of the mark and the contents of the tag are ignored.

e. tag after the body was assessed or ignored call tag processor doEndTag () method to return the rest of the page EVAL_PAGE then be evaluated, return SKIP_PAGE the Servlet code immediately from _JSPService () returned.

4. Body tag processor interface and implementation

Tag javax.Servlet.JSP.tagext.BodyTag is sub-interfaces.

javax.Servlet.JSP.tagext.BodyTagSupport achieve BodyTag class.

BodyContent is javax.Servlet.JSP.JSPWriter subclass, but distinct from its parent class.

BodyContent the content of the object is not automatically written into the Servlet output stream, but the accumulation of a string in the cache. When the label after the completion of the target body still in the doEndTag () method can be applied by the getString () or getReader () method of operation. And, where necessary, modify and write recovery JSPWriter output stream.

BodyContent class methods

public void flush () throws IOException replication JSPWrite.flush () method so that it always produces overflow. Refresh write longer valid, because it is not connected to the actual output will be written to the stream.

public void clearBody () reset BodyContent cache is empty.

public Reader getReader () return Reader reads the body content.

public void writeOut (Write w) to write the body contents of the specified output.

public JSPWrite getEnclosing Write () returns the stack is written by the next higher object (may be another BodyContent object).

BodyTag interface defines a new integer constant

EVAL_BODY_TAG When the doStartTag () returns, making new BodyContent object is created and associated with this tag handler. When doAfterBody () returns, so JSP Servlet modifying the label control in any variable assessed again after the body.

BodyTag interface method

public void setBodyContern (BodyContent out) in the current JSPWriter has been written, a new BodyContent after being created by the JSP Servlet call, it occurs in doStartTag () after.

public void doInitBody () throws JSPException setBodyContent (), the body is called life cycle assessment method before. If multiple assessment body, this method is called only once.

public init doAfterBody () throws JSPException body was assessed, BodyContent writer is still active when the call of the life cycle approach. This method must return EVAL_BODY_TAG or SKIP_BODY, if returned EVAL_BODY_TAG assessed again when the body.

BodyTagSupport class methods

public int doStartTag () throws JSPException replication TagSupport in the doStartTag () method.

public int doEndTag () throws JSPException call TagSupport in the doEndTag () method returns the result.

public void setBodyContent (BodyContent out) in a protected member variable bodyContent a new body to save the contents of the object, sub-class can directly access this object.

public void doInitBody () throws JSPException default do nothing. Is need to implement sub-classes initialize the replication.

public int doAfterBody () throws JSPException assessment for each body was called by the JSP Servlet, physical object with the contents still is active. Back SKEP_BODY or EVAL_BODY_TAG the body be evaluated again

public void release () set the bodyContent object to null, then call super.release ().

public BodyContent getBodyContent () return bodyContent variable. Subclass has access to protected variables, but this method allows independent label deal with this body content type to send the output.

public JSPWriter getPreviousOut () call in the bodyContent variable getEnclosingWriter () and easy way to return the results.

5. Body tag processor lifetime

a. generate Servlet need to create an instance of tag handler class. Implementations call the JSP container is usually a factory class method, factory class instance pool contains a tag processor so that it can reuse the object is no longer active.

b. Initialize label processor, Servlet informed of its presence. Servlet label processor by calling two methods to achieve this process: setPageContext (PageContext ctx) and setParent (Tag parent).

c. If the tag has attributes, attribute values passed through the processor to provide setter methods to the object. Property setter method is a label the only way to support the required attributes.

d. the page context and parent label has been transferred home and already have property. Call label processor doStartTag () method, which can read these variables and perform the necessary functions to achieve the calculation of standard A and operation.

doStartTag () method must return an integer.

鈼?Back EVAL_BODY_TAG the normal processing tag body (skip to e);

鈼?Back SKIP_BODY from the initial JSP page until the end of mark and the contents of the tag are ignored. (Skip to f)

e. If the return EVAL_BODY_TAG, then the normal processing tag body.

e1. in the stack to save the current JSPWriter object, create a new BodyContent object, and its set out for the JSP page context object stored in the context of the property named name. With its setBodyContent () method.

e2. call doInitBody () method to initialize.

e3. processing tag body. Write output BodyContent object, this process depends on the TLD label element, there are three possible values.

e4. call doAfterBody () method, in vivo body content is written JSPWriter, can be achieved as follows:

鈼?JSPWriter out = bodyContent.getEnclosingWriter ();

鈼?out.println (bodyContent.getString ());// bodyContent.writeOut (out);

鈼?bodyContent.clear ();

e5.doAfterBody () method returns two possibilities:

鈼?Back EVAL_BODY_TAG, the re-evaluation of the tag body, which is being recycled array and enumerate the typical situation.

鈼?Back SKIP_PAGE, to continue the remainder of the page.

e6. body of content completed, the process of creating it was the reverse:

Call pageContent.popBody () method to retrieve the object in front of JSPWriter. Those settings will be written back out implicit object.

f. tag after the body was assessed or ignored calls doEndTag () method, allowing label processor as the output stream back to the content.

Back EVAL_PAGE the rest of the page being evaluated;

Back SKIP_PAGE the Servlet code immediately from _JSPService () returned.

g. At this point the body content in a protected bodyContent object still available. To incorporate the Servlet output stream:

鈼?JSPWriter out = pageContext.getOut ();

鈼?out.println (bodyContent.getString ()); or

鈼?bodyContent.WriteOut (pageContext.getOut ());

6. Tag category Additional information

4, the label instructions

taglib directive aims to specify the TLD element location, set on the page with the label to distinguish a short alias.

Syntax:

Attribute: prefix: tag library used to identify unique identifier. uri: tag library's own URI.

uri need not point to an actual file, which is in the web.xml JSP container can find the actual file location of the unique identifier.







相关链接:



Picked Games Kids



Limit of WMA format music



Versed in pop-up window for ASP.NET



How to convert mp3 to aac



Shuo-side by side with the day THINKING up the computer "settle down"



E-Mail Clients introduction



Let your XP into a Mac OS X



Flv Format



how to Convert mp3 to mp4



Avi Converter



Tencent behind the Opponent who already 51.com



"Fallout 3" aircraft carrier task of the city of clues and details of the Extension



Blue Wave eras invited to attend the China Shipbuilding Heavy Industry "transfer mode" Expert Group