1. Custom tags: Java classes that implement specific interfaces and encapsulate predefined behaviors written in Java code.
* During runtime, the tag is replaced with the corresponding predefined Java code.
* The JSP custom tag provides a method to replace simple JavaBean and Java scripts.
Better yet, a set of standardized User-Defined tag libraries already exist in jstl.
* The purpose is to separate the business and presentation logic, and the code is reusable and portable.
2. Tag Library: a set of custom tags grouped by function or implementation.
1. API: three interfaces and two classes in the javax. servlet. jsp. tagext package
2, composition:
* Tag handler class
* Tag library descriptor File
* Application deployment descriptor
* JSP file
3. custom tag steps:
1. Implementation: implement the interface or class defined by the tagext package;
2. Configuration: configure the TLD file;
3. Association: configure the Web. xml file;
3. Tag handler class: An Implementation class for custom tags;
1. The tag handler is called during runtime. One of the three Java interfaces defined in the javax. servlet. jsp. tagext package must be implemented or extended.
* Tag interface: basic protocols for tag handlers and JSP pages. Defines the basic methods for all tag handlers.
* Iterationtag interface: extends the tag interface to control repeated processing of tags.
* Bodytag interface: inherits the iterationtag interface to process the content in the TAG body.
2. Default Implementation of interfaces: tagsupport and bodytagsupprt class:
* The tagsupport class is the default Implementation of the tag and iterationtag interfaces. Supports Simple labels and labels with subject iterations.
* The bodytagsupprt class is the default implementation of the bodytag interface. Tags of the subject content that need to be accessed and operated are supported.
3. Implement the lifecycle of the tag handler class for the tag interface:
1. setpagecontext (): called by the JSP Container before the tag processor. Set the Page Context of the tag and call the setparent () method to set the tag's parent tag. If not, set it to null;
Setparent ()
2. Set the attributes of a tag: If the tag has an attribute, call the setxxx method to set the tag attribute. If the tag has no attribute, skip this step.
3, dostarttag (): returns eval_body_include or skip_body
Eval_body_include: the output TAG body to the current output stream.
Skip_body: Ignore the label body
4, doendtag (): returns eval_page or skip_page
Eval_page: remaining part of the execution page
Skip_page: ignore the rest of the page
5. Container cache label processor class instances, reuse cached labels to process class instances
6. Release (): release an instance,
4. lifecycle of the label handler class that implements the iterationtag interface:
1, setpagecontext ()
Setparent ()
2. Set tag attributes.
3, dostarttag (): returns eval_body_include or skip_body
Eval_body_include:
Execute the TAG body (executed once );
Call doafterbody (): Return eval_body_after or skip_body;
Eval_body_after: Jump to the front and re-execute the label body
Skip_body:
Skip_body:
4, doendtag (): eval_page or skip_page
Eval_page: remaining part of the execution page
Skip_page:
5, starting from 1
6. Release ()
5. lifecycle of the label handler class implementing the bodytag interface:
1, setpagecontext ()
Setparent ()
2. Set tag attributes.
3, dostarttag (): returns eval_body_buffered or skip_body_include, skip_body.
Eval_body_buffered and the label body is not null:
Setbodycontent ():
Set the bodycontent attribute of the tag processor class, and provide the bodycontent object to cache static and dynamic content of the TAG body.
Call doinitbody () to prepare for the label body execution
Jump to the execution TAG body:
Skip_body_include: Jump directly to the execution TAG body:
Skip_body: Ignore the label body and jump directly to 5.
4. Execute the TAG body (after being executed again );
Call doafterbody (): Return eval_body_after or skip_body;
Eval_body_after: Jump to the front and re-execute the label body
Skip_body:
Skip_body: No repeated execution
5, doendtag (): eval_page or skip_page
Eval_page: remaining part of the execution page
Skip_page:
6, starting from 1
7. Release ()
6. tagsupport class: supports Simple labels and labels with subject iteration.
7. bodytagsupprt class: supports tags for accessing and operating the subject content of tags.
8. bodycontent class: cache output content.
* Create a jspwriter class instance on the JSP Container processing page, and introduce all outputs to the instance (static and dynamic content ),
If the "custom tag implementation class" of the JSP Container implements the bodytag interface, all the output cached in the jspwriter instance will be temporarily relocated to the bodycontent class instance for caching, until the custom tag ends.
Therefore, bodycontent is a subclass of the jspwriter class, And the JSP tag processor can read the content in the TAG body through the getstring () method of this object.
Public void flush () throws ioexception rewrites the jspwrite. Flush () method so that it always produces overflow. The refresh write is invalid because it is not connected to the actual output stream to be written.
Public void clearbody () resets the bodycontent cache to null.
Public reader getreader () returns the body content read by reader.
Public String getstring () returns the content in the TAG body.
Public void writeout (write W) writes the body content to the specified output.
Public jspwrite getenclosingwrite () returns the next higher writer object in the stack (possibly another bodycontent object ).
9. lifecycle of the JSP Tag Processor
1. Initialize an instance: Create a tag instance and call all the setting methods (setpagecontext, setparent and all attribute setting methods) to initialize the instance.
2. Call the dostarttag () method to set the instance variable to a valid value only for the current call. If the method is processing the label body of an element, the doendtag method is called.
3. The tag instance is reused. If the attribute has different values, the corresponding setting method is called to repeat the 2 operation. The tag processor can reuse an instance only when it has the same property set.
4. Use the release method to release internal resources occupied by the tag processor.
10. Note:
* Provides default values for "properties.
* Because the container cache label processor class instances are used repeatedly, the cached label processing class instances are used.
Therefore, after changing a class variable, using this class variable will also be affected.
Therefore, pay attention to the concurrent access issue.
Therefore, you need to reset the call status each time. The best place is in the dostarttag method.
* The tag will never call release () during the call ();
* Do not use the bodycontent object in the setbodycontent method or the doinitbody method. It is only used to obtain the object and prepare the object.
* If you use empty or non-empty tags for custom tags that implement the bodytag interface, an exception is thrown. Because empty labels do not call some methods.
4. Tag library Descriptor (TLD) file: TLD is a library consisting of tags used on JSP pages for tag configuration;
1. The tag library descriptor file is an XML document describing a tag library. It contains information such as the tag name, attribute, tag processing class, and tag attributes.
2. The extension is. TLD and must be placed under the WEB-INF directory.
<Taglib version = "2.0"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 1.2 </JSP-version>
<Short-Name> short name of a tag </short-Name>
<Tag>
<Name> tag name </Name>
<Tag-class> complete tag processing class name </Tag-class>
<Body-content> MARK whether the subject can embed content and how the content is processed. Empty cannot be used. </body-content>
</Tag>
</Taglib>
Tag Element configuration instructions:
<Tag>
<Name> tagnmae </Name> ----- name of the tag
<Tagclass> package. Class </tagclass> ----- This tag is implemented by that class (this class can be found in the Struts. jar package)
<Bodycontent> Empty </bodycontent> ----- the tag can be ended directly without entering the content.
Here, bodycontent has three optional values.
JSP: the TAG body consists of other JSP elements.
If there is a JSP element, the tag will first explain and then pass in the actual value of the element. For example, if the TAG body contains JSP elements such as <% = attributename %>, the tag will be passed in according to the actual value of attributename. This is the most common one.
Empty: the label body must be empty.
When referencing this tag, you can <Bean: Write bundle = "attributename"/> without <Bean: Write bundle = "attributename"> </Bean: Write>
Tagdependent: interpreted by tags without JSP Conversion
<Attribute> ----- a parameter of the tag is identified here.
<Name> attrname </Name> -- Name of the Parameter
<Required> false </required> ----- this parameter is required. If it is true, this parameter must be set. Otherwise, an error is returned.
<Rtexprvalue> true </rtexprvalue> ------ indicates whether the value of this attribute can receive runtime expressions. Rtexprvalue: "run-time expression value", whether the value can be dynamically assigned, such as value = "<% = attributename %>" in JSP"
</Attribute>
</Tag>
6. Create a reference between the JSP page and the tag Library: static reference and dynamic reference, and associate tags;
1. You can declare a static reference through the Web application Descriptor (Web. XML,
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<JSP-config>
<Taglib>
<Taglib-Uri>/mytags </taglib-Uri>
<Taglib-location>/WEB-INF/mytld. TLD </taglib-location>
</Taglib>
</JSP-config>
</Web-app>
Then, add the JSP Declaration to all pages on which the User-Defined tag library needs to be used:
<% @ Taglib uri = "mytags" prefix = "ABC" %>
Note that the specified URI attribute matches the taglib-Uri value specified in the web. xml file.
2. You can declare a dynamic reference directly on the page.
You only need to add a JSP Declaration to all pages that need to use this library:
<% @ Taglib uri = "/WEB-INF/lib/datetaglib. TLD" prefix = "ABC" %>
3. Comparison between static reference and dynamic reference:
During static reference of the tag library, the JSP declaration must query the Web. xml file to execute the database query.
This means that if you move or rename the library, or want to add more libraries to the Web. xml file,
You must stop the server, update the web. xml file, and restart the server.
The dynamic method allows the JSP page to direct to the TLD location, so it is processed when the JSP page is interpreted.
Static methods provide a certain degree of directness between the actual names and locations of pages and libraries,
This allows you to change these attributes without modifying the page. On the other hand, dynamic methods provide greater flexibility,
This allows you to add and move tag declarations at runtime. If you are interested in dynamic methods,
However, I am worried that after some changes, it may be necessary to update the maintenance burden of multiple pages,
You can always put the JSP declaration in a separate JSP file, and add this page to every page of the custom library to access the Web application.
This allows you to increase the flexibility of the database by updating the information once during running.
7. Development Collaboration
1. Use the displayed parent-child collaboration: provide parameters for the parent tag through the Child tag in the inner layer to form a complete or complementary function implementation. For example, <JSP: include> child element in Action Element <JSP: param>.
* Use the tagsupport. findancestorwithclass (this, paramparent. Class) method to obtain the reference of the parent tag in the Child tag.
* Call the parent label method to pass the subclass parameters to the parent class.
* A parent tag is an interface that defines how parameters in a sub-tag are passed to the parent tag for the sub-tag to call.
2. Use implicit collaboration with variables:
Implicitly collaborate by setting JSP scope variables. One action uses the processing result as a variable in a JSP scope, and the other action uses the variable as its input. For example, iterative behavior.
8. Use the listener in the tag Library:
Use the <listener> element after <validator> to define the listener implementation for your library in the TLD.
When a container loads a web application, it traverses all the TLD to find the listener definition and registers the listener.
9. Data Type Conversion
1. the JSP Container automatically converts the basic data type from the text value to the property value.
2. Use propertyeditor to convert the text value to any Java data type.
10. Exception Handling
If a JSP element throws an exception, the default processing is to send it to an error page.
However, special tags (such as file operations) must be displayed. The new interface trycatchfinally of jsp1.2 is used to handle exceptions.
If any of the elements in the nested behavior bodies, such as dostarttag (), doendtag (), doinitbody (), or doafterbody, throws an exception.
The container calls the docatch method. We can record the problem in this method and throw our own exception. The page will continue to process other parts.
The dofinally method is always called by the container. If an exception is thrown in the dostarttag method, the output information of the printwriter object cannot be created. Therefore, check null.