JSP custom tags

  • 2020-05-10 18:37:38
  • OfStack

1. Basic concepts:

1. The label (Tag) :

The tag is an XML element that makes the JSP web page simple and easy to maintain. It also makes it easy to support multiple language versions of the same JSP file. Because the tag is an XML element, its name and attributes are case sensitive

2. Label library (Tag library):

A collection of 1 series of functionally similar and logically related tags is called a tag library.

3. Label library description file (Tag Library Descriptor):

The tag library description file is an XML file that provides the mapping between the classes in the tag library and the tag references in JSP. It is a configuration file and is similar to web.xml.

4. Label processing class (Tag Handle Class):

The tag handler class is an Java class that inherits the TagSupport interface or extends the SimpleTag interface. This class enables you to customize the JSP tags

2. Customize the format of JSP tags:

1.

< % @taglib prefix= "someprefix" uri= "/sometaglib" % >


In order for the JSP container to be able to use the custom behavior in the tag library, the following two conditions must be met:

1) identify the tag representing this custom behavior from a specified tag library

2) find the concrete classes that implement these custom behaviors

The first requirement - finding out which tag library a custom behavior belongs to - is done by the tag directive prefix (Taglib Directive's Prefix) attribute, so elements that use the same prefix on the same page belong to the tag library. Each tag library defines a default prefix that is used to insert custom tags into a document or page in the tag library. So, you can use in addition to such as jsp jspx, java, servlet, sun, sunw (they are specified in the white paper JSP reserved words) such as the prefix.

The uri property meets the second requirement above. Find a class for each custom behavior. This uri contains a string that the container USES to locate the TLD file. The names of all the tag handling classes in the tag library can be found in the TLD file

2. When the web application starts, the container searches for all files ending in.tld from the META-INF 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 relationship for each TLD file and the corresponding URI.

On the JSP page, we only need to match the specific tag library by using the tag library directive with the URI attribute value

3. Process of custom JSP label:

1. Introduction of tag libraries in JSP:

< % @taglib prefix= "taglibprefix" uri= "tagliburi" % >


2. Tag library tags are used in JSP

3. The Web container gets the uri attribute value of taglib declared in step 1 according to prefix in step 2

4. The Web container finds the corresponding element 5 in web.xml according to the uri attribute. Get the value of the corresponding element from the element 6. Web container finds the corresponding.tld file 7 from the WEB-INF/directory according to the value of the element. Find the element 8 corresponding to tagname from the.tld file. The Web container creates an instance of tag handle class based on the value of the element. The Web container calls the doStartTag/doEndTag method of this instance to complete the corresponding processing

4. Basic steps for creating and using 1 Tag Library:

1. Create a tag handler class (Tag Handler Class)

2. Create the label library description file (Tag Library Descrptor File)

3. Configure elements in web.xml. 4. Import tag libraries in JSP

5. Introduction to class TagSupport:

1. Handle label class must extend javax. servlet. jsp. TagSupport.

2. Main attributes of class TagSupport:

A.parent attribute: represents the processing class of the upper tag that contains the current tag

B. pageContex attributes: for the javax Web application. servlet. jsp. PageContext object

3. Before calling doStartTag or doEndTag methods, the JSP container calls setPageContext and setParent methods, setting pageContext and parent. So you can access the pageContext variable directly in the tag processing class

4. The pageContext member variable cannot be accessed in the TagSupport constructor because the JSP container is not yet invoked

The setPageContext method initializes pageContext

6. How TagSupport handles labels:

1. The TagSupport class provides two methods for processing tags:

public int doStartTag() throws JspException
public int doEndTag() throws JspException

2.doStartTag: but when the JSP container encounters the start flag of the custom tag, it calls the doStartTag() method.

The doStartTag() method returns an integer value used to determine the subsequent flow of the program.

A.Tag.SKIP_BODY: > ... The content between is ignored

B.Tag.EVAL_BODY_INCLUDE: indicates that the contents between the tags are executed normally

3.doEndTag: but when the JSP container encounters the end flag of the custom tag, it calls the doEndTag() method. The doEndTag() method also returns an integer value used to determine the subsequent flow of the program.

A.Tag.SKIP_PAGE: indicates that the page is stopped immediately, and that the unprocessed static content on the page and the JSP program are ignored. Any existing output is immediately returned to the client's browser.

B.Tag_EVAL_PAGE: indicates that the JSP page continues to execute according to the normal process

7. User-defined label properties:

If a custom property is also included in the tag, it should be used as a member variable in the tag processing class and provided with methods to set and read the property, respectively.

8. Steps to create a label processing class:

1. Create a file containing the static text of the JSP web page (that is, the text to replace the custom JSP tag)

2. Load static text when the Web application starts

3. Create a tag processing class

9. How to create a file containing JSP web page static text:

1. Use the java.util.Properties class to hold the static text to replace the custom JSP tags on the page

2. The Properties class represents a collection of 1 series of properties, and the example can be either saved or loaded from the stream. The text is stored in the WEB-INF directory in the form of key/value, for example key=value, and in the property list these key/value are of type String

10. Common API of class Properties:

1.setProperty(String key, String value) : call the put method of Hashtable class to add properties

2.getProperty(String key) : gets the property value corresponding to key in the property list

3.load(InputStream in) : read the property list from the input stream object InputStream (Properties list)

4.store(OutputStream out,String coMMent) : write the property list property pairs to the output stream object in the appropriate format. By default, ISO-88590-1 encoding is used to process the input as lines. The property key/value is paired with "=, :" and separated by carriage return, line feed, key/value

101. Common API of the 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 stream can be data in any form

102. How to read and save properties files using ServletContxt:

1. Create java.util.Properties class object

2. Get the ServletContext object

3. Read the properties file as an input stream into an input stream object

4. Load the input stream object into the Properties object

5. Save the Properties object to the ServletContext object

103. 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 when configuring this Servlet in web.xml:

someclass

somepackage.SomeClass1

2. Create the java.util.Properties class in the Servlet init() method

3. Get the ServletContext object currently applied by Web

4. Read the properties file in 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 on

context.setAttribute("attributeName",ps);

104. How to create a tag handler class:

1. Bring in the necessary resources:

import javax.servlet.jsp.*;

import javax.servlet.http.*;

import java.util.*;

import java.io.*;

2. Inherit the TagSupport class and override the doStartTag()/doEndTag() methods

3. Get the java.util.Properties object from the ServletContext object

4. Get the corresponding property value of key from the Properties object

5. Process the acquired attributes accordingly and output the results

Create the label library description file (Tag Library Descriptor) :

1. The label library description file, TLD for short, USES the XML file format to define the user's label library. The elements in the TLD file can be divided into three categories:

A. : tag library element

B. : tag element

C. : tag attribute element

2. Tag library element is used to set tag library information, and its common attributes are:

A.shortname: specify the default prefix name for Tag Library (prefix)

B.uri: sets the 1-only access symbol for Tag Library

3. The tag element is used to define a tag. Its common attributes are:

A.name: sets the name of Tag

B. tagclass: sets the processing class of Tag

C.bodycontent: sets the body (body) content of the label

1).empty: means there is no body in the label

2).JSP: the JSP program code can be added to the body of the label

3).tagdependent: means that the content in the tag is handled by the tag itself

4. The tag attribute element is used to define the attributes of the tag. Its common attributes are:

A.name: property name

B.required: if the property is required, false is the default

C.rtexprvalue: can the attribute value be request-time expression, which is similar to < % =... % > The expression of

106. Use of labels in Web applications:

1. If a custom JSP tag is used in an Web application, an element must be added to the web.xml file to declare the tag library where the referenced tag resides

/sometaglib

/WEB-INF/someTLD.tld

2. : set the unique 1 identifier of Tag Library, which will be used to reference Tag Libray in Web applications

3. : specify the location of the TLD file corresponding to Tag Library

4. Need to be added in JSP file < % @ taglib% > Directive to declare a reference to the tag library. Such as:

< % @taglib prefix = "somePrefix" uri = "/someuri" % >

5.prefix is the prefix used to refer to the tag library in the JSP web page. uri is used to specify the identifier of Tag Library, which must be 1 to the attribute in web.xml.


Related articles: