Custom Label for JSP

Source: Internet
Author: User
Tags tld

First, the basic concept:

1. Tags (tag):

A label is an XML element that enables a JSP Web page to be concise and maintainable, and to easily implement the same JSP file to support multiple language versions. Because the label is an XML element, its name and attributes are case-sensitive

2. Tag libraries (Tag library):

A set of functionally similar, logically interrelated tags is called a tag library.

3. Tag library description file (Tag descriptor):

The tag library description file is an XML file that provides a mapping of the label references in the class and JSP in the tag library. It is a configuration file, and Web.xml is similar.

4. Label processing class (Tag Handle Class):

The label processing class is a Java class that inherits the TagSupport or extends the Simpletag interface, which enables you to implement the specific functions of a custom JSP tag

Second, the custom JSP label format:

1.

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

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

1 Identify the label representing this custom behavior from a specified tag library

2 Find specific classes to implement these custom behaviors

The first requirement-find a custom behavior that belongs to that tag library-is done by the prefix of the label directive (Taglib directive ' s Prefix), 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 in a document in a tag library or in a page to insert a custom label. So, you can use prefixes other than those such as JSP,JSPX,JAVA,SERVLET,SUN,SUNW (which 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 name of all label processing classes in the tag library can be found in the TLD file

2. When the Web application starts, the container searches all files at the end of the. TLD from the Meta-inf of the directory structure of the Web-inf folder. That means they locate all the TLD files. For each TLD file, the container first obtains the URI of the tag library, and then creates a mapping relationship 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 instruction with the URI attribute value

Third, the custom JSP label processing process:

1. To introduce a tag library in a JSP:

<% @ taglib prefix= "Taglibprefix" uri= "Tagliburi"%>

2. Using tag library labels in your JSP

3. The Web container obtains the URI attribute value of the taglib declared in the first step, based on the prefix in the second step

4. The Web container finds the corresponding element 5 in web.xml based on the URI property. Gets the value of the corresponding element from the element 6. The Web container finds the corresponding. tld file 7 from the web-inf/directory based on the value of the element. Find the element 8 corresponding to the tagname from the. tld file. The value 9 of the corresponding element is obtained in the rounding element. The Web container creates instance 10 of the corresponding tag handle class based on the value of the element. The Web container invokes the Dostarttag/doendtag method of this instance to complete the corresponding processing

The basic steps to create and use a tag library:

1. Create label processing class (Tag Handler Class)

2. Create Tag library description file (tag Descrptor file)

3. Configure element 4 in the Web.xml file. Fetching tag libraries in JSP files

Five, TagSupport class introduction:

1. The class that handles the label must extend the Javax.servlet.jsp.TagSupport.

Main properties of the 2.TagSupport class:

A.parent Property: A processing class that represents an upper-level label that has nested current labels

B.pagecontex property: Represents a Javax.servlet.jsp.PageContext object in a Web application

The 3.JSP container invokes the Setpagecontext and SetParent methods before invoking the doStartTag or Doendtag method, setting PageContext and parent. So you can access PageContext variables directly in the label processing class

4. PageContext member variables cannot be accessed in the TagSupport construction method because the JSP container has not yet called

The Setpagecontext method initializes the PageContext

Six, the TagSupport processing label method:

The 1.TagSupport class provides two ways to process labels:

public int doStartTag () throws Jspexception
public int Doendtag () throws Jspexception

2.doStartTag: The doStartTag () method is invoked when the JSP container encounters the starting flag of a custom label.

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

A.tag.skip_body: To say .... The content between is ignored

B.tag.eval_body_include: Indicates that the contents between tags are properly executed

3.doEndTag: The Doendtag () method is invoked when the JSP container encounters the end flag of a custom label. The Doendtag () method also returns an integer value to determine the program's subsequent process.

A.tag.skip_page: To stop the execution of Web pages immediately, static content on the Web page and JSP programs are ignored any existing output immediately returned to the customer's browser.

B.tag_eval_page: Indicates that the JSP Web page continues to execute in the normal process

Seven, user-defined label properties:

If you also include custom attributes in the label, you should use this property as a member variable in the label processing class and provide a method for setting and reading the property, respectively.

The steps to create a label processing class:

1. Create a file that contains static text for a JSP Web page (that is, the text to replace the custom JSP label)

2. Load static text when a Web application starts

3. Create a label processing class

How to create a file that contains static text for a JSP Web page:

1. Use the Java.util.Properties class to hold static text (3lian footage) to replace the custom JSP label in the Web page

The 2.Properties class represents a collection of properties that can either be saved to the stream or loaded from the stream. The text is stored in a key/value form in the Web-inf directory, such as Key=value, which are string types in the property list

Ten, properties of the common API:

1.setProperty (string key, String value): Call the Put method of the Hashtable class to add a property

2.getProperty (String key): Get property value for key in attribute list

3.load (InputStream in): Reading property list from input stream object InputStream

4.store (OutputStream out,string coMMent): Use the appropriate format to write property list properties to the output stream object, by default using the ISO-88590-1 encoding format, processing input in rows. The key/value of the attribute is paired with "=,:", and the Key/value pair is separated by a carriage return, a newline

Xi. Common APIs for ServletContext classes:

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 files parameter

4.getRequestDispatcher (String Path): Returns the Requestdispacher object represented by path

5.getResourceAsStream (String Path): Returns the resource corresponding to the path as an input stream, in which the object can be any form of data, and the path parameter must start with "/" and relative to the context Root

12. How to use Servletcontxt to read and save Property Files: (www.3lian.com)

1. Create Java.util.Properties class objects

2. Get ServletContext Object

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

4. Load an input stream object into a Properties object

5. Save the Properties object to the ServletContext object

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.