Custom tags)
(Prepared study notes)
Step 1. Create a Tag handler class (Tag Processor class)
Package javax. servlet. jsp. tagext
Develop a label processor class for an empty Body -----> 1) implements Tag 2) extends TagSupport
The method of overwriting the label processor class of the empty Body is as follows:
DoStartTag ()
Returned value: Tag. SKIP_BODY
DoEndTag ()
Return Value:
1) Tag. EVAL_PAGE: Keep the content under the Tag.
2) Tag. SKIP_PAGE: Disable the content under the Tag.
Development of the label processor class with Body -----> 1) implements BodyTag 2) extends BodyTagSupport
Method to overwrite the label processor class with Body
DoStartTag ()
Return Value:
BodyTag. SKIP_BODY: Call the doEndTag () method directly after execution.
BodyTag. EVAL_BODY_INCLUDE: Call the doAfterBody () method directly after execution.
BodyTag. EVAL_BODY_BUFFERED:
DoEndTag ()
Return Value:
BodyTag. EVAL_PAGE: The content under the tag continues to be executed.
BodyTag. SKIP_PAGE: The content under the tag is not executed.
DoAfterBody ()
Return Value:
BodyTag. SKIP_BODY: Call the doEngTag () method after execution
BodyTag. EVAL_BODY_AGAIN: Call the doAfterBody () method after execution.
Step 2. Create a tld file (tag library descriptor)
An XML file used to describe tags
Usually placed in a WEB-INF directory (/WEB-INF/*. tld)
Abc. tld
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE taglib
PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.2 // EN"
Http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd>
<! -- Template test -->
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<Jsp-version> 1.2 </jsp-version>
<Short-name> tagSample </short-name>
<Uri>/tagSample </uri>
<Description> A blank tag library template. </description>
<Tag>
<Name> welcome </name>
<Tag-class> tagapp. HelloTag </tag-class>
<Body-content> empty </body-content>
<Display-name> my first tag </display-name>
<Small-icon> </small-icon>
<Large-icon> </large-icon>
<Description> </description>
<Attribute>
<Name> name </name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
<Type> java. lang. String </type>
</Attribute>
</Tag>
</Taglib>
Step 3. Create a JSP file
<% @ Taglib uri = "[tld_url]" prefix = "" %>
<% @ Taglib uri = "/WEB-INF/abc. tld" prefix = "mike" %>
...
<Mike: welcome name = ""/>
The uri value can be:
A,/WEB-INF/mytag. tld -----> uri = "/WEB-INF/mytag. tld"
B. <uri>/tagSample </uri> ------> uri = "/tagSample" in the tld File"
C. Map the value of the tld file to taglib-uri using the web. xml file -----> uri = "/abc"
<Taglib>
<Taglib-uri>/abc </taglib-uri>
<Taglib-location>/WEB-INF/mytag. tld </taglib-location>
</Taglib>
Comment on the method of the label processor class with Body:
DoStartTag ()
Return value: SKIP_BODY empty BODY tag
EVAL_BODY_INCLUDE indicates the BODY, and the execution process enters doAfterBody.
EVAL_BODY_BUFFERED carries the BODY tag, and the execution process enters setBodyContent
DoEndTag ()
Return value: SKIP_PAGE ignores the remaining JSP content after the tag
Remaining JSP content after EVAL_PAGE continues to run the tag
DoAfterBody ()
Return value: SKIP_BODY ends the doAfterBody () method, and the process enters the doEndTag ()
EVAL_BODY_AGAIN continues to execute the doAfterBody () method
SimpleTagSupport
Good exercise questions:
I,
1. Develop the following labels by imitating the jstl tag Library:
1) <SQL: setDataSource>
Attributes:
Driver: required. An expression can be used.
Url; required. An expression can be used.
User: required. An expression can be used.
Password: required. An expression can be used.
2) <SQL: query>
Attributes:
DataSource: required. An expression can be used.
SQL: required. expressions can be used.
3) <SQL: update>
Attributes:
DataSource: required. An expression can be used.
SQL: required. expressions can be used.
2. Develop a tag similar to <c: forEach>. You can traverse a set object. (optional. If it cannot be completed, you can directly use the jstl tag)