One, custom Tags: Java classes that implement a specific interface encapsulate the predefined behavior written by Java code.
* At run time, the label is replaced with the corresponding predefined Java code.
* JSP Custom tags provide a way to replace simple JavaBean and Java scripts.
Better yet, there is already a set of defined standard custom tag libraries in JSTL.
* The purpose is to separate the business from the presentation logic, the reusability and portability of the code.
Second, Tag library: A collection of custom labels grouped by function or implementation.
Three interfaces and 2 classes in the 1,api:javax.servlet.jsp.tagext package
2, Composition:
* Tag Handler class
* Tag Library Descriptor file
* Application Deployment Descriptor
* JSP file
3, custom Label steps:
1, implementation: the implementation of TAGEXT package defined interface or class;
2, Configuration: Configure TLD file;
3, association: Configure Web.xml file;
Third, tag handler class: A custom label implementation class;
1, the label handler is invoked at run time. One of the three Java interfaces defined by the Javax.servlet.jsp.tagext package must be implemented or extended.
* Tag Interface: The basic protocol for tag handlers and JSP pages. Defines the basic method for all tag handlers.
* Iterationtag interface: Extend the tag interface, control the duplication of the label body processing.
* Bodytag Interface: Inherits the Iterationtag interface, handles the contents of the label body.
2, the default implementation of the interface: TagSupport and BODYTAGSUPPRT classes:
* The TagSupport class is the default implementation of the tag and Iterationtag interfaces. Supports simple labels and labels with body iterations.
* The Bodytagsupprt class is the default implementation of the Bodytag interface. Supports labels that require access to and manipulate the contents of the label body.
3, the tag interface to implement the label Handler class life cycle:
1,setpagecontext (): Called by the JSP container before the label processor. Sets the page context of the label, calls the SetParent () method to set the parent tag of the label, and none is set to null;
SetParent ()
2, set the label properties: There are properties, call the Setxxx method to set the label properties, no properties are skipped.
3,dostarttag (): Return to Eval_body_include or skip_body
Eval_body_include: Output tag body to current output stream
Skip_body: Ignore label body
4,doendtag (): Return to Eval_page or skip_page
Eval_page: Perform the rest of the page
Skip_page: Ignore the rest of the page
5, container cache tag processor class instance, repeated use of cached label processing class instance
6,release (): Frees up instances,
4, implementation of the Iterationtag interface tag handler class life cycle:
1,setpagecontext ()
SetParent ()
2, set the properties of the label
3,dostarttag (): Return to Eval_body_include or skip_body
Eval_body_include:
Execute the label body (to be executed again);
Call Doafterbody () method: Return Eval_body_after or Skip_body;
Eval_body_after: Jump to front of the back-execute tab body
Skip_body:
Skip_body:
4,doendtag (): Eval_page or Skip_page
Eval_page: Perform the rest of the page
Skip_page:
5, starting from 1
6,release ()
5, implementation of the Bodytag interface tag handler class life cycle:
1,setpagecontext ()
SetParent ()
2, set the properties of the label
3,dostarttag (): Returns eval_body_buffered or Skip_body_include,skip_body.
Eval_body_buffered and the label body is NOT NULL:
Setbodycontent ():
Sets the Bodycontent property of the label processor class, provides a Bodycontent object, caches static content and dynamic content of the label body.
Call Doinitbody () Prepare for label body execution
Jump to execution label body:
Skip_body_include: Jump directly to the execution label body:
Skip_body: Ignore the tab body and jump to 5 directly
4, execute the label body (after being executed again);
Call Doafterbody () method: Return Eval_body_after or Skip_body;
Eval_body_after: Jump to front of the back-execute tab body
Skip_body:
Skip_body: No repeat execution
5,doendtag (): Eval_page or Skip_page
Eval_page: Perform the rest of the page
Skip_page:
6, starting from 1
7,release ()
6,tagsupport class: Supports simple tags and tags with body iterations.
7, BODYTAGSUPPRT class: supports the label that needs to access and manipulate the contents of the label body.
8,bodycontent class: Caching output content.
* JSP container Process page Check Create JspWriter class instance, bring all output into the instance (static and dynamic content behavior),
If the custom tag implementation class encountered by the JSP container implements the Bodytag interface, all output cached in the JspWriter instance is temporarily relocated to the Bodycontent class instance cache until the custom label ends.
So Bodycontent is also a subclass of the JspWriter class, and the JSP tag processor can also read the contents of the tag body through the object's GetString () method.
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.
The public String getString () returns the contents of the label body.
The 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).
The lifecycle of the 9,JSP label processor
1, initialize the instance: Create the label instance, invoke all the setting methods (Setpagecontext,setparent method and all property's setting method) 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 element's label body, the Doendtag method is invoked.
3, the label instance is reused, and if the attribute has different values, the corresponding setting method is called, and the action of 2 is repeated. The label processor reuses an instance only if it has the same collection of attributes.
4, use the release method to let the label processor free up internal resources.
10, note:
* Provides default values for properties.
* Repeated use of cached tags to handle class instances because the container caches the label processor class instance.
So after you change the class variable, you will also be affected by using the class variable later.
Therefore, pay attention to the problem of concurrent access.
So, each time you reset the state of the call. The best place is the doStartTag method.
* The label will never call Release () during the call;
* Do not use the Bodycontent object in the Setbodycontent method and the Doinitbody method, only to get the object and do the preparation work.
* For a custom label that implements the Bodytag interface, with both an empty label and an empty label, an exception is thrown. Because empty tags do not call some methods.
The tag Library descriptor (TLD) file: The TLD is a library of tags used in JSP pages to configure labels;
1, the tag library descriptor file is an XML document that describes a tag library, containing information such as the name of the tag, attributes, the attributes of the label processing class, and the label.
2, the extension is. TLD, which must be placed in 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> Label for short </short-name>
<tag>
<name> Label's name </name>
<tag-class> label processing class complete name </tag-class>
<body-content> tag body can embed content, and how content is handled. You cannot use empty</body-content>
</tag>
</taglib>
The configuration description for the tag element:
<tag>
<name>tagNmae</name>-----The tag's name
< Tagclass>package. Class</tagclass>-----This tag is implemented by that class (which can be found in the Struts.jar package)
<bodycontent>empty</ bodycontent>-----This tag can end directly without needing to fill out the content
here Bodycontent has three optional values
jsp : The tag body consists of other JSP elements
If it has a JSP element, the label interprets it first, and then the actual value of the element is passed in. For example, the tag body contains a <%=attributeName%> such a JSP element, at this time the label will press AttributeName the actual value of what is passed in. This is the most commonly used one.
empty: The label body must be empty
When quoting this tag, you can <bean:write bundle= "AttributeName"/> without having to <bean:write bundle= "AttributeName" ></bean:write>
tagdependent: Explained by labels, Convert without JSP
<attribute>-----Here is an argument for this tag.
<name>attrname</name>--the name of this parameter
<required>false</required>-----Whether this parameter is required, if true, you must write this parameter, or you will report an error.
<rtexprvalue>true</rtexprvalue>------is to say whether the value of this property can receive an expression at run time. Rtexprvalue: "Run-time EXPRESSION value", whether it can be dynamically assigned, in JSP such as value= "<%=attributeName%>"
</attribute>
</tag>
Set up a reference between JSP page and tag library: Static reference and dynamic reference, associated label;
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 that need to use the Custom tag library:
<%@ taglib uri= "mytags" prefix= "abc"%>
Note The URI attribute specified matches the Taglib-uri value specified in the Web.xml file.
2, you can declare a dynamic reference directly in the page.
Just add a JSP declaration to all pages that need to use this library:
<%@ taglib uri= "/web-inf/lib/datetaglib.tld" prefix= "abc"%>
3, static references are compared to dynamic references:
When you make a static reference to a tag library, the JSP declaration must query the Web.xml file to execute the library query.
This means that if you move or rename the library, or you want to add more libraries to the Web.xml file,
You must stop the server, update the Web.xml file, and then restart the server.
The dynamic method makes the JSP page point directly to the TLD location and is processed when the JSP page is interpreted.
Static methods provide a certain degree of directness between the actual name and location of the page and the library,
This gives you some flexibility to change these properties without modifying the page. On the other hand, dynamic methods provide greater flexibility,
Allows you to add and move tag declarations at run time. If you are interested in dynamic methods,
But also worry about making some changes, it is possible to update the maintenance burden of multiple pages,
You can always place the JSP declaration in a separate JSP file and add this page to each page in the custom library where you want to access the WEB application.
This allows you to increase the flexibility of the library at run time only if you need to update the information once.
Seven, develop collaborative behavior
1, using the display of parent-child collaboration: through the inner layer of the child tag for the parent tag to provide parameters to form a complete or complementary function implementation; For example, the child element in the <jsp:include> action element <jsp:param>.
* Use the Tagsupport.findancestorwithclass (this, Paramparent.class) method to get a reference to the parent tag in a child label.
* The method that invokes the parent tag passes the arguments of the subclass to the parent class.
* The parent tag implements a specific interface in which the parameters in the child tag are passed to the parent tag for child tag invocation.
2, using implicit collaboration through variables:
Implicitly collaborate by setting variables for the JSP scope. One behavior handles the result as a variable in one of the JSP scopes, while the other uses the variable as its input. such as iterative behavior.
Use the listener in the tag library:
Use the <Listener> element to define the listener implementation for your library in the TLD after <validator>.
When the container loads the Web application, it traverses all the TLD to find the listener definition and registers the found listener.
Nine, data type conversion
The 1,jsp container automatically handles conversions from the literal value to the base data type of the property value.
2, convert using PropertyEditor: Converts a literal value to any Java data type.
X. Handling of exceptions
If the JSP element throws an exception, the default processing for it is sent to an error page.
However, processing must be shown for special tags such as file operations. JSP1.2 new interface Trycatchfinally interface to handle exceptions specifically.
If the element in the nested behavior is doStartTag (), Doendtag (), Doinitbody (), or any one of the Doafterbody methods throws an exception.
The container invokes the Docatch method. We can record the problem in this method and throw our own exception. The page will continue to handle other parts as well.
The Dofinally method is always invoked by the container. When an exception is thrown in the doStartTag method, the PrintWriter object output information cannot be created, so it is checked for null.