Java Web Learning notes (5)-custom JSP labels

Source: Internet
Author: User
Tags tld
I. Basic concepts:
**************************************** **************************************** *****
1. Tag ):
A tag is an XML element. A tag can simplify and maintain a JSP webpage, and supports multiple language versions for the same JSP file. Because tags are XML elements, their names and attributes are case sensitive.

2. Tag Library ):
A set of tags that have similar functions and are logically associated with each other is called a tag library.

3. Tag library description file (TAG library descriptor ):
The tag library description file is an XML file that provides the ing between the class in the tag Library and the tag reference in JSP. It is a configuration file, which is similar to Web. xml.

4. Tag handle class ):
The tag processing class is a Java class that inherits the tagsupport or extends the simpletag interface. This class can be used to customize the specific functions of JSP labels.
**************************************** **************************************** *****

Ii. Custom JSP tag format:
**************************************** **************************************** *****
1. <% @ taglib prefix = "someprefix" uri = "/sometaglib" %>
To enable the JSP Container to use the custom behavior in the tag library, the following two conditions must be met:
1. Identify the tag representing this custom row from a specified tag Library
2. Find the specific class to implement the custom Behavior

The first required condition-Identify the tag library that a custom behavior belongs to-is completed by the taglib directive's prefix attribute of the tag command, therefore, all elements with the same prefix on the same page belong to this tag library. Each tag library defines a default prefix, which is used to insert custom tags in the document or page of the tag library. Therefore, you can use prefixes such as JSP, jspx, Java, Servlet, sun, and sunw (they are all reserved words specified in the JSP White Paper.
 
The URI property meets the second requirement. Find the corresponding class for each custom behavior. This URI contains a string that the container uses to locate the TLD file. In the TLD file, you can find the names of all tag processing classes in the tag library.

2. <someprefix: sometag key = "somevalue"/>
When a web application starts, the container searches for all files ending with. TLD from the WEB-INF of the directory structure of the META-INF folder. That is to say, they will locate all TLD files. For each TLD file, the container first obtains the URI of the tag library, and then creates a ing relationship between each TLD file and the corresponding Uri.
On the JSP page, we only need to use the tag library command with the URI property value to match the specific tag library.
**************************************** **************************************** *****

3. Process custom JSP labels:
**************************************** **************************************** *****
1. Introduce the tag library in JSP: <% @ taglib prefix = "taglibprefix" uri = "tagliburi" %>
2. Use the tag library tag in JSP: <Prefix: tagname attribute = "tagattribute">
3. The Web Container obtains the URI attribute value of taglib declared in the first step based on the prefix in the second step.
4. The Web Container finds the corresponding <taglib> element in Web. XML based on the URI attribute.
5. Obtain the value of the <taglib-location> element from the <taglib> element.
6. Web containers find the corresponding. TLD file from the WEB-INF/directory based on the value of the <taglib-location> element
7. Find the <tag> element corresponding to the tagname from the. TLD file.
8. Obtain the value of the <tag-class> element from the <tag> element.
9. The Web Container creates an instance of the corresponding tag handle class based on the value of the <tag-class> element.
10. The Web Container calls the dostarttag/doendtag method of this instance to complete corresponding processing.
**************************************** **************************************** *****

4. Basic Steps for creating and using a tag Library:
**************************************** **************************************** *****
1. Tag handler class)
2. Create a tag library description file (TAG library descrptor file)
3. Configure the <tag> element in the web. xml file.
4. Add a tag Library to the JSP file
**************************************** **************************************** *****

V. Introduction to tagsupport:
**************************************** **************************************** *****
1. The class for processing tags must extend javax. servlet. jsp. tagsupport.
2. Main attributes of the tagsupport class:

A. Parent attribute: indicates the processing class of the Upper-layer labels nested with the current tag.
B. pagecontex attribute: indicates the javax. servlet. jsp. pagecontext object in the Web application.

3. before calling the dostarttag or doendtag method, JSP containers call the setpagecontext and setparent methods to set pagecontext and parent. Therefore, you can directly access the pagecontext variable in the tag processing class.

4. The pagecontext member variable cannot be accessed in the tagsupport constructor, because the JSP Container has not called
The setpagecontext method initializes pagecontext.
**************************************** **************************************** *****

6. How tagsupport handles tags:
**************************************** **************************************** *****
1. The tagsupport class provides two methods to process tags:
Public int dostarttag () throws jspexception
Public int doendtag () throws jspexception

2. dostarttag: but when the JSP Container encounters the starting flag of the custom tag, it will call the dostarttag () method.
The dostarttag () method returns an integer to determine the subsequent process of the program.
A. Tag. skip_body: Indicates <Prefix: sometag>... Content between </Prefix: sometag> is ignored
B. Tag. eval_body_include: indicates that the content between tags is executed normally.

3. doendtag: however, if the JSP Container encounters the end mark of the custom tag, the doendtag () method is called. Doendtag
() The method also returns an integer to determine the subsequent process of the program.
A. Tag. skip_page: indicates that the webpage is stopped immediately. The unprocessed static content and JSP programs on the webpage are
Suddenly. Returns any existing output content to the client's browser immediately.
B. tag_eval_page: indicates that the JSP page continues to be executed according to the normal process.
**************************************** **************************************** *****

7. custom tag attributes:
**************************************** **************************************** *****
If a tag contains custom attributes, for example:
<Prefix: mytag attribute1 = "value1">
...
</Prefix: mytag>
This attribute should be used as a member variable in the tag processing class, and the methods for setting and reading attributes are provided respectively.
**************************************** **************************************** *****

8. Steps for creating a tag processing class:
**************************************** **************************************** *****
1. Create a file containing static JSP webpage text (that is, the text to replace the custom JSP tag)
2. Load Static text at web application startup
3. Create a tag processing class
**************************************** **************************************** *****

9. How to create a file containing static JSP webpage text:
**************************************** **************************************** *****
1. Use the java. util. properties class to store static text to replace custom JSP labels on webpages
2. The properties class represents a set of attributes. Its instances can be saved to the stream or added from the stream.
. These texts are stored in the WEB-INF directory as key/value, such as key = value, in the property list
These keys/values are of the string type.
**************************************** **************************************** *****

10. Common properties APIs:
**************************************** **************************************** *****
1. setproperty (string key, string value): Call the put method of the hashtable class to add attributes.
2. getproperty (string key): Get the property value corresponding to the key in the property list
3. Load (inputstream in): Read the properties list from the inputstream object)
4. Store (outputstream out, string comment): Write the attribute pairs in the attribute list in the appropriate format
In an out-of-Stream object, input is processed in rows by default in ISO-88590-1 encoding format. Attribute key/value
Pairs are separated by "=" and ":", and key/value pairs are separated by carriage return and line feed.
**************************************** **************************************** *****

11. Common APIs of the servletcontext class:
**************************************** **************************************** *****
1. getcontext (string uripath): returns the servletcontext object represented by uripath on the server.
2. getinitparameter (string name): returns the value of the name parameter in the servletconfig object.
3. getminetype (string file): returns the MIME type of the object represented by the file parameter.
4. getrequestdispatcher (string path): returns the requestdispacher object represented by path.
5. getresourceasstream (string path): The resource corresponding to the path is returned in the form of an input stream. Objects in the input can be in any form of data. The path parameter must start with "/" and be relative to the context root.
**************************************** **************************************** *****

12. How to Use servletcontxt to read and save attribute files:
**************************************** **************************************** *****
1. Create a java. util. properties Class Object
2. Get the servletcontext object
3. Read the attribute file into an input stream object as an input stream.
4. Load the input stream object to the properties object
5. Save the properties object to the servletcontext object.
**************************************** **************************************** *****

13. How to load static text at web application startup:
**************************************** **************************************** *****
1. Create a subclass that inherits the httpservlet class. Set the load-on-startup attribute When configuring the servlet in Web. xml:
<Servlet>
<Servlet-Name> someclass </servlet-Name>
<Servlet-class> somepackage. someclass </servlet-clas
<Load-on-startup> 1 </load-on-startup>
</Servlet>
2. Create the java. util. properties class in the init () method of the servlet.
3. Obtain the servletcontext object of the current Web Application
4. Read the property file under the WEB-INF Directory into the input stream inputstream:
Inputstream in = context. getresourceasstring ("WEB-INF/someproperties. properties ");
5. Load the input stream to the property object
PS. Load (in );
6. Save the property object to the context.
Context. setattribute ("attributename", PS );
**************************************** **************************************** *****

14. How to Create a tag processing class:
**************************************** **************************************** *****
1. introduce necessary resources:
Import javax. servlet. jsp .*;
Import javax. servlet. http .*;
Import java. util .*;
Import java. Io .*;
2. inherit the tagsupport class and overwrite the dostarttag ()/doendtag () method
3. Obtain the java. util. properties object from the servletcontext object.
4. Get the property value corresponding to the key from the properties object
5. process the obtained attributes and output the results.
**************************************** **************************************** *****

15. Create a tag library description file (TAG library descriptor ):
**************************************** **************************************** *****
1. The tag library description file, TLD for short, defines the user's tag library in XML format. The elements in a TLD file can be divided into three types:
A. <taglib>: Tag library Element
B. <tag>: Tag Element
C. <attribute>: Tag attribute Element

2. The tag library element <taglib> is used to set information about the tag library. Its common attributes include:
A. shortname: Specify the default prefix name (prefix) of the tag library)
B. Uri: Set the unique access identifier of the tag library.

3. The Tag Element <tag> is used to define a tag. Its common attributes include:
A. Name: Set the tag name.
B. tagclass: Set the tag processing class.
C. bodycontent: Set the subject content of the tag.
1). Empty: indicates that there is no body in the tag.
2). jsp: indicates that JSP code can be added to the TAG body.
3). tagdependent: indicates that the content in the tag is processed by the tag itself.

4. The tag attribute element <attribute> is used to define the attributes of a tag. Its common attributes include:
A. Name: attribute name
B. Required: required or not. The default value is false.
C. rtexprvalue: whether the attribute value can be a request-time expression, which is similar to <% =... %> Expression
**************************************** **************************************** *****

16. Use tags in Web applications:
**************************************** **************************************** *****
1. If a custom JSP tag is used in a web application, the <taglib> element must be added to the web. xml file to declare the tag library of the referenced tag.
<Taglib>
<Taglib-Uri>/sometaglib </taglib-Uri>
<Taglib-location>/WEB-INF/sometld. TLD </taglib-location>
</Taglib>

2. <taglib-Uri>: Set the unique identifier of the tag library, which will be used to reference the tag libray in Web applications.
3. <taglib-location>: specify the location of the TLD file corresponding to the tag library.
4. Add the <% @ taglib %> command to the JSP file to declare the reference to the tag library. For example:
<% @ Taglib prefix = "someprefix" uri = "/someuri" %>

5. prefix indicates the prefix when the tag of the tag library is referenced in the JSP webpage. The URI is used to specify the tag library identifier, which must be consistent with the Web. the <taglib-Uri> attributes in XML are consistent.
**************************************** **************************************** *****

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.