Custom tag library

xiaoxiao2021-04-07  374

v Custom Tag Library

Ø Custom Tag Library allows you to encapsulate logical, and use these logic in format familiar with web designers.

Ø Using a custom tag library, you can reduce the JAVA code in the JSP page to the least, making the entire application easier

Debugging and maintenance.

Ø Tag extension foundation

ª Custom Behavior is a tag processor class for custom behavior, which is a bean in essentially.

ª Marking processor class must implement one of three Java interfaces defined by JSP specification.

i) Tag: Define how to implement it in any behavior

ITERATIONTAG: Increases the method required for an iteration in behavior

III) Bodytag: How to increase method behavior

ª Implementing all interfaces and classes needed to mark the processor are defined in the javax.servlet.jsp.tagext package.

ª In order to make the development mark processor easier, the API defines two interfaces that inherited, and supported.

i) Tagsupport: The default implementation of the method for the TAG and ITERATIONTAG interface

II) Bodytagsupport: The default implementation of the method for the BodyTag interface

In practice, the markup processor should be achieved to the extension of support classes.

ª ª ª 库

i) Tag processor class

II) Tag library descriptor file, TLD: XML file, map all custom behavior names to the appropriate tag processor class. Moreover, the properties supported by custom behavior are described.

ª ª The custom behavior element in the JSP page is made by a start tag (possibly containing attributes), a behavior and one

End marker consists of.

The body

ª If the behavioral element is not active, you can use a short-write form to start tag and end tags.

ª Marking processor class implements the behavior of this behavior.

Attr1 = "Value

1 "-> setattr1 (" Value

1")

Attr2 = "Value

2 "-> setattr1 (" Value

2")

> -> DOSTARTTAG ()

The body

-> doendtag ()

ª Tag processor needs access to all information about the request and scope, and this property value (eg

There are words. The container will call the method defined in the TAG interface to provide this information.

ª Implement the tag interface to add dynamic content to the subject, set the first part, add

Plus or delete variables in a JSP scope and tell the container to include behavioral elements in your response,

Or ignore this behavior.

ª The following is the most important TAG interface method

© Public Void SetPageContext (PageContext PageContext);

This method is called by the JSP container before using the tag processor. PageContext provides a request

And response objects, and access to all variables in the JSP scope.

© public int dostarttag () throws jspexceptioin;

The JSP container will call the dostartTag () method when you encounter the start tag. return value:

I) Skip_body: Tell the JSP container ignore the entire behavior.

II) Eval_body: Tell the JSP container to process behavior (eg, scripting elements and other behavioral elements in executing behavior), and put the result into an answer.

© Public INT Doendtag () THROWS JSPEXCEPTION;

No matter what the dostarttag () method returns, when the end tag of the corresponding behavior element is encountered, JSP

The container will call the DOENDTAG () method.

return value:

i) Skip_page: Tell the JSP container to abandon the processing of the rest of the page.

This value can be used if the tag processor is forwarded to another page, or if you want to send a redirect response to your browser.

II) EVAL_PAGE: Tell the JSP container to continue to process the rest of the page

© © Marking Properties Attributes should be set in custom behavior, otherwise the JSP container refuses to

This page.

ª Require container to repeat the behavior of behavioral elements, need to implement the ITERATIONTAG interface, it only contains one

method:

© Public Int Doafterbody Throws JSPEXCEPTION

This method is called by the container after the container has a behavior of behavioral elements.

© For the ITERATIONTAG interface, after calling the dostarttat () method, the DOENDTAG () method is called in the final call.

The DOAFTERBODY () method is also called before.

Attr1 = "Value

1 "-> setattr1 (" Value

1")

Attr2 = "Value

2 "-> setattr1 (" Value

2")

> -> DOSTARTTAG ()

The body -> doaferbody ()

-> doendtag ()

© where the dostarttag () method can return Eval_Body_include, which tells the container to

A answer to the behavior content of the cycle element, then call DOAFTERBODY ().

© DoafterBody () method You can return EVAL_BODY_AGAIN (iterative in behavior), or return

Skip_body (to stop iteration).

© When the DOAFTERBODY () method returns Skip_body, the container calls the DOENDTAG () method.

ª Read and process the marker processor of the actor, need a bodytag interface

© BodyTag inherits the ITERATIONTAG interface, but also adds two new methods

i) public void setbodycontent (bodycontent bodycontent)

II) public void doinitbody () THROWS JSPEXCEPTION

Attr1 = "Value

1 "-> setattr1 (" Value

1")

Attr2 = "Value

2 "-> setattr1 (" Value

2")

> -> DOSTARTTAG ()

The body -> setBodyContent ()

-> DOINITBODY ()

-> DOAFERBODY ()

-> doendtag ()

© The dostarttat () method here can return Eval_Body_TAG, which means that not only behavior should

The process must be processed, and the container must also make the result of the processing can be used by the marker processor, only the implementation

The class of the bodytag interface, Eval_body_tag, is valid.

© BodyContent object to buffer behavior content

© BodyContent provides several tool methods

i) getBodyContent ()

Returns a reference to the BodyContent object, and its content is read by getString () method.

II) getReader () method

Get the contents of Reader

III) getpreviousout () method

Returns the main JSPWriter

ª TrycatchFinalLinally interface

© When DostartTag (), DoeendTag (), DONITBODY () or DOAFTERBODY () method throws an exception, and the container calls the docathc () method. The DOFINALLY () method always has a container call. If it is normal, after doendtag (), if an abnormality occurs, it is after the DofinalLinally () method.

ª If the behavior introduces a variable, then external to the tag processor, you also need to use the TAGEXTRAINFO subclass, the optional element to specify a fully qualified class name of the TAGEXTRAINFO subclass. In JSP1.2, this class is rarely used.

ª General should generally replace the TAGEXTRAINFO subclass, and use new elements

ª Universal library elements

© : Elements that contain behavioral elements

© : There are 3 possible values

i) EMPTY: The behavior must be empty

II) JSP: Behavior includes JSP elements, JSP elements in behavior are processed, this value is default

III) TagDependent: Means JSP elements in behavior do not need to be handled

© : Provides information about the variables of custom behavior as a script variable.

© Elements Each attribute of a behavior contains elements.

© , it is the same as the value of the element, TRUE: The expression is specified by the request.

Sex value, default is False

© element can specify the Java type of the property.

ª Make up the package and installation of the tag library (TLD and all class files).

© In the development process, the tag library class file can be placed in Web-INF / CLASSES, and TLD must be an extension.

The named.tld file can be stored in Web-INF / TLDS.

© After the development, you can package all tags and TLDs into a JAR file. This makes it

The installation library is easy. At this time, the TLD must be stored as an extension .tld file in the meta-inflicity of the JAR file, such as meta-inf / taglib.tld. Then put the JAR file in

The web-inf / lib directory.

© JSP1.2, when the web application starts, the container scans the entire web-INF directory structure to look for

.tld file and find the JAR file with .tld files in the meta-inflicity, the container will look for

TLD's element and create a mapping from the URI to the TLD containing it.

© Before JSP1.2, you need to manually define it in Web.xml.

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

New Post(0)