Custom Label tutorial for JSP

Source: Internet
Author: User
Tags integer 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.

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. Introduce tag library in JSP:

2. Use tag library label in JSP

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

The 4.Web container finds the corresponding element in web.xml based on the URI property 5. The value of the corresponding element from the element the 6.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 tagname from the. tld file. The value of the corresponding element in the rounding element 9.Web container creates an instance of tag handle class 10 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 store static text that you want to replace a custom JSP label in a 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:

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

13. How to load static text when a Web application starts:

1. Create a subclass that inherits the HttpServlet class and set the Load-on-startup property when this servlet is configured in Web.xml:

SomeClass

Somepackage. SomeClass1

2. Create the Java.util.Properties class in the Init () method of this servlet

3. Get the ServletContext object for the current Web application

4. Read the property files in the Web-inf directory into the input stream InputStream:

InputStream in = context.getresourceasstring ("web-inf/someproperties.properties");

5. Load an input stream into a Property object

Ps.load (in);

6. Save the Property object to the top

Context.setattribute ("AttributeName", PS);

14. How to create a label processing class:

1. Introduction of 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. Get the Java.util.Properties object from the ServletContext object

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

5. Processing the acquired attributes and outputting the results

Create Tag library description file (Tag descriptor):

1. Tag library description file, referred to as TLD, using XML file format, defines the user's tag library. Elements in a TLD file can be grouped into 3 categories:

A.: Tag Library elements

B.: Label elements

C.: Label Property elements

2. The tag library element is used to set the relevant information of the tag library, and its common properties are:

A.shortname: Specifies the tag library default prefix name (prefix)

B.uri: Set Tag Library's unique access notation

3. The label element is used to define a label, and its common properties are:

A.name: Set Tag's name

B.tagclass: Set tag's processing class

C.bodycontent: Set the subject (body) content of the label

1). Empty: Indicates that there is no body in the label

2). JSP: Indicates that the body of the tag can be added to the JSP program code

3). Tagdependent: Indicates that the contents of the label are handled by the label itself

4. Tag property elements are used to define the properties of a label, and its common properties are:

A.name: Property Name

B.required: property is required, default is False

C.rtexprvalue: Whether the property value can be an request-time expression, that is, an expression similar to <%=...% >

16, use the label in the Web application:

1. If a custom JSP tag is used in a Web application, you must include an element in the Web.xml file that declares the tag library where the referenced label is located

/sometaglib

/web-inf/sometld.tld

2.: Set tag Library's unique identifier, in the Web application will be based on it to reference tag Libray

3.: Specify the location of the TLD file corresponding to the tag library

4. Need to join in JSP file

5.prefix represents the prefix for referencing the label of this tag library in a JSP Web page, which is used to specify the identifier of the tag library and must be consistent with the attributes in the Web.xml.

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.