In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will give you a detailed explanation on how to customize the JSP tag. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
1. Basic concepts:
1. Tags (Tag): tags are XML elements that make JSP pages concise and easy to maintain, and you can easily support multiple language versions of the same JSP file. Because the tag is a XML element, its name and attributes are case-sensitive
two。 Tag library (Tag library): a collection of similar and logically related tags is called a tag library.
3. Tag library description file (Tag Library Descriptor): the tag library description file is an XML file that provides a mapping between classes in the tag library and tag references in JSP. It is a configuration file, similar to web.xml.
4. Tag handling class (Tag Handle Class): the tag handling class is a Java class that inherits TagSupport or extends the SimpleTag interface, through which you can implement the specific functions of custom JSP tags
Second, customize the format of JSP tags:
In order for the JSP container to use custom behavior in the tag library, the following two conditions must be met:
1. Identify tags that represent this custom behavior from a specified tag library
two。 Find the specific class that implements these custom behaviors * the necessary conditions-to find out which tag library a custom behavior belongs to-is done by the Taglib Directive's Prefix attribute of the tag instruction, so elements that use the same prefix on the same page belong to this tag library. Each tag library defines a default prefix that is used to insert custom tags into the document or page of the tag library. So, you can use prefixes such as jsp,jspx,java,servlet,sun,sunw (they are all reserved words specified in the JSP white paper).
The uri attribute satisfies the second requirement above. Find the corresponding class for each custom behavior. This uri contains a string that the container uses to locate the TLD file. The names of all tag handling classes in the tag library can be found in the TLD file.
When the web application starts, the container searches for all files ending in .tld from the META-INF of the directory structure of the WEB-INF folder. That is, they will locate all the TLD files. For each TLD file, the container first gets the URI of the tag library, and then creates a mapping for each TLD file and the corresponding URI. In the JSP page, we only need to match the specific tag library by using the tag library directive with the value of the URI attribute
3. The process of customizing JSP tags:
1. Introduce tag library into JSP:
2. Use tag library tags in JSP:
According to the prefix in the second step, the 3.Web container obtains the uri attribute value of taglib declared in the * * steps.
The 4.Web container finds the corresponding element in web.xml according to the uri attribute
5. Get the value of the corresponding element from the element
The 6.Web container finds the corresponding .tld file from the WEB-INF/ directory according to the value of the element.
7. Find the element corresponding to tagname from the .tld file
8. Get the value of the corresponding element in the hash element
The 9.Web container creates an instance of the corresponding tag handle class based on the value of the element
10. The Web container calls the doStartTag/doEndTag method of this instance to complete the corresponding processing
4. The basic steps for creating and using a Tag Library:
1. Create a processing class for tags (Tag Handler Class)
two。 Create a tag library description file (Tag Library Descrptor File)
3. Configure elements in the web.xml file
4. Introducing tag Library in JSP File
5. Brief introduction of Tagsupport class:
1. Classes that handle tags must extend javax.servlet.jsp.TagSupport.
The main attribute of the 2.TagSupport class: the A.parent attribute: represents the processing class B.pageContex attribute of the upper tag nested with the current tag: represents the javax.servlet.jsp.PageContext object in the Web application
Before calling the doStartTag or doEndTag methods, the 3.JSP container calls the setPageContext and setParent methods to set pageContext and parent. Therefore, the pageContext variable can be accessed directly in the tag handling class
4. PageContext member variables cannot be accessed in the constructor of TagSupport because the JSP container has not yet called the setPageContext method to initialize pageContext
6. TagSupport's method of dealing with tags:
The 1.TagSupport class provides two methods for handling tags: public int doStartTag () throws JspException public int doEndTag () throws JspException
2.doStartTag: but when the JSP container encounters the start flag of a custom tag, it calls the doStartTag () method. The doStartTag () method returns an integer value that is used to determine the subsequent flow of the program. A.Tag.SKIP_BODY: denote...
The content between is ignored B.Tag.EVAL_BODY_INCLUDE: indicates that the content between tags is executed normally
3.doEndTag: but when the JSP container encounters the end flag of a custom tag, it calls the doEndTag () method. The doEndTag () method also returns an integer value that is used to determine the subsequent flow of the program. A.Tag.SKIP_PAGE: indicates that the execution of the web page is stopped immediately, and any unprocessed static content and JSP programs on the web page are ignored and any existing output is immediately returned to the customer's browser. B.Tag_EVAL_PAGE: indicates that the JSP web page continues to be executed according to the normal process
7. User-defined tag attributes: if custom attributes are included in the tag, for example:.
Then you should use this property as a member variable in the tag processing class, and provide methods to set and read the property, respectively.
Eighth, the steps to create a tag processing class: 1. Create a file that contains the static text of the JSP page (that is, the text to replace the custom JSP tag) 2. Load static text 3. 0 when the Web application starts. Create a label handling class
9. How to create a file that contains static text of a JSP web page:
1. Use the java.util.Properties class to store static text to replace custom JSP tags in a web page
The 2.Properties class represents a collection of properties whose instances can be saved or loaded from the stream. The text is stored in the WEB-INF directory in the form of key/value, such as key=value, and these key/value are of type String in the attribute list
10. Common API of Properties class:
1.setProperty (String key, String value): call the put method of the Hashtable class to add properties
2.getProperty (String key): gets the attribute value corresponding to key in the attribute list
3.load (InputStream in): reads the list of attributes from the input stream object InputStream (Properties list)
4.store (OutputStream out,String coMMent): writes the attribute pairs of the attribute list in the appropriate format to the output stream object, using the ISO-88590-1 encoding format by default, and processes the input as lines. The key/value of the attribute is paired with "=,:", and the key/value pairs are separated by carriage return and line feed
11. Common API of ServletContext class:
1.getContext (String uripath): returns the ServletContext object represented by uripath in 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 file represented by the file parameter
4.getRequestDispatcher (String path): returns the RequestDispacher object represented by path
5.getResourceAsStream (String path): returns the resource corresponding to path in the form of an input stream. The object in the input retention can be any form of data, and the path parameter must start with "/" and relative to Context Root.
12. How to use ServletContxt to read and save property files:
1. Create a java.util.Properties class object
two。 Get ServletContext object
3. Read the properties file into an input stream object in the form of an input stream
4. Load the input stream object into the Properties object
5. Save the Properties object to the ServletContext object
13. How to load static text when the Web application starts:
1. Create a subclass that inherits the HttpServlet class, and set the load-on-startup property: someclass somepackage.SomeClass1 when configuring the Servlet in web.xml
two。 Create the java.util.Properties class in the init () method of the Servlet
3. Get the ServletContext object of the current Web application
4. Read the properties file under the WEB-INF directory into the input stream InputStream: InputStream in = context.getResourceAsString ("WEB-INF/someproperties.properties")
5. Load the input stream into the property object ps.load (in)
6. Save the property object to the context. Context.setAttribute ("attributeName", ps)
14. How to create a tag handling class:
1. Bring in the necessary resources: import javax.servlet.jsp.*; import javax.servlet.http.*; import java.util.*; import java.io.*
two。 Inherit the TagSupport class and override the doStartTag () / doEndTag () method
3. Get the java.util.Properties object from the ServletContext object
4. Get the attribute value corresponding to key from the Properties object
5. Process the acquired attributes and output the results
Create a tag library description file (Tag Library Descriptor):
1. Tag library description file, referred to as TLD, uses the XML file format to define the user's tag library. The elements in the TLD file can be divided into three categories: A.: tag library element B.: tag element C.: tag attribute element
two。 The tag library element is used to set the information about the tag library. Its common attributes are: A.shortname: specify the default prefix name of Tag Library (prefix) B.uri: set the unique access token of Tag Library
3. Tag element is used to define a tag, its common attributes are: A.name: set the name of Tag B.tagclass: set the processing class of Tag C.bodycontent: set the body of the tag (body) content 1) .empty: indicates that there is no body in the tag 2) .JSP: the body that represents the tag can be added to the JSP program code 3) .tagdependent: indicates that the content in the tag is handled by the tag itself
4. Tag attribute element is used to define the attribute of the tag. Its common attributes are: A.name: attribute name B.required: whether the attribute is required. The default is false C.rtexprvalue: whether the attribute value can be a request-time expression, that is, an expression similar to that.
16. Use tags in Web applications:
1. If custom JSP tags are used in Web applications, you must add an element to the web.xml file that declares the tag library / sometaglib / WEB-INF/someTLD.tld where the referenced tag is located
2. Set the unique identifier of Tag Library, which will be used to reference Tag Libray in the Web application
3. Specify the location of the TLD file corresponding to Tag Library
4. You need to add an instruction in the JSP file to declare a reference to the tag library.
5.prefix represents the prefix when referencing the tag of this tag library in a JSP page, and uri is used to specify the identifier of the Tag Library, which must be consistent with the attributes in web.xml.
On "how to customize JSP tags" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 246
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.