Friday, October 8, 2010

Jsp custom tag in depth from shallow to explain in detail



1, the basic concepts:

1. Tag (Tag):

Tag is an XML element, the tag can get simple JSP pages and easy to maintain, you can easily achieve the same JSP file to support multiple languages. As the label is the XML element, its name and attributes are case-sensitive

2. Tag Library (Tag library):

By a series of similar functions, logically interconnected as the set of tags tag library.

3. Tag library descriptor (Tag Library Descriptor):

Tag library description file is an XML file, this document provides a JSP tag library in the middle class and the quote on the label mapping. It is a configuration file, and web.xml is similar.

4. Label processing type (Tag Handle Class):

Tag processing class is a Java class that inherits or extends SimpleTag TagSupport interface can be achieved through this class custom JSP tag specific functions

Second, the format of the custom JSP tags:

1. <% @ Taglib prefix = "someprefix" uri = "/ sometaglib"%>

Order to make the JSP container to use a custom tag library behavior must meet the following two conditions:

1. From a specified tag library to identify acts on behalf of this custom tag

2. To find these custom actions to achieve a specific class


The first requisite for - to find a custom tag library that behavior is - is prefixed by the label instructions (Taglib Directive's Prefix) property, and therefore in the same page elements using the same prefix belong to this tag library. Each tag library defines a default prefix, used in the tag library in the document or page, insert a custom label. So, you can use in addition to, such as jsp, jspx, java, servlet, sun, sunw (they are specified in the JSP White Paper, reserved words) of the class prefix.

uri attribute the above to meet the second requirement. Find custom behavior for each corresponding class. This includes a string uri, container use it to locate TLD file. TLD file can be found in the tag library in the name of all labels dealing with class

2.

When the web application starts, the container from the WEB-INF folder META-INF directory structure to search for all. Tld at the end of the file. That they will locate all of the TLD file. For each TLD file, the container will first obtain the tag library URI, and then for each TLD file and create a mapping between the corresponding URI.

In the JSP page, we need only with a URI attribute value by using the tag library and specific instructions to match the tag library

Third, custom JSP tag processing:

1. Introduced in the JSP tag library:

2. Using the JSP tag library tags:

3. Web container according to the second step in the prefix, was declared the first step in the uri attribute value taglib

4. Web container under the uri attribute to find the corresponding element in the web.xml

5. Elements obtained from the corresponding value of the element

6. Web container according to the value of the element from the WEB-INF / directory to find the corresponding. Tld file

7. From. Tld file, find the corresponding element tagname

8. Minato element in the corresponding element of the value obtained

9. Web container according to the value of the element to create the appropriate tag handle class instance

10. Web container calls the instance of the doStartTag / doEndTag method to complete the appropriate treatment



4, a Tag Library to create and use the basic steps:


1. To create tag handling class (Tag Handler Class)

2. Create a tag library description file (Tag Library Descrptor File)

3. Configuration elements in the web.xml file

4. In the JSP tag library is introduced into the document

5, TagSupport Class Description:

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

2.TagSupport main categories of attributes:

A.parent attributes: representative of the current tag nested treatment of the upper class label

B.pageContex property: Representative Web applications javax.servlet.jsp.PageContext object

3.JSP containers or doEndTag method before calling doStartTag, will first call setPageContext and setParent methods, set pageContext and parent. Therefore, in dealing with the class label can directly access pageContext variable

4. TagSupport the constructor can not access pageContext member variables, because at this time do not call the JSP container

setPageContext method to initialize pageContext

6, TagSupport handling labeling method:

1.TagSupport class provides two methods of dealing with labels:

public int doStartTag () throws JspException

public int doEndTag () throws JspException


2.doStartTag: JSP container, but the initial encounter signs custom label will call the doStartTag () method.

doStartTag () method returns an integer value, used to determine the procedures for follow-up process.

A.Tag.SKIP_BODY: that between the content is ignored ...

B.Tag.EVAL_BODY_INCLUDE: that between the contents of the tag is working properly


3.doEndTag: but the custom tag JSP container encountered the end of signs, it will call the doEndTag () method. doEndTag

() Method also returns an integer value, used to determine the procedures for follow-up process.

A.Tag.SKIP_PAGE: that an immediate stop to the implementation of web pages, static content pages and JSP untreated procedures were

Suddenly. Any existing output slightly immediately returned to the client browser.

B. Tag_EVAL_PAGE: that the normal JSP page process to continue

7, user-defined tag attributes:

If the label also contains a custom attribute, such as:

...

So the label should be dealing with this property class as member variables, and to set and read properties respectively method.

8, create a label deal with classes of steps:

1. Create a JSP page that contains a static text file (that is to replace the custom JSP tags to text)

2. In the Web application starts loading static text

3. To create tag handling class

9, how to create a JSP page that contains a static text file:


1. Using the java.util.Properties class to store custom Web pages to replace the static text in JSP Tags

2.Properties class collection of representatives of a number of properties, examples of both can be saved to the stream, can also increase stream

Set. The text to key / value form stored in the WEB-INF directory, such as key = value, the attribute list

These key / value is of type String

10, Properties class common API:


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

2.getProperty (String key): get the list of key properties of the corresponding attribute values

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

4.store (OutputStream out, String comment): Use the appropriate format property list of the attributes of the written input

The stream object, the default use of ISO-88590-1 encoding format to handle the line input. Attribute key / value of

Matching between the to "=,:" to carriage return, newline key / value pairs

11, ServletContext class of Common API:

1.getContext (String uripath): return server uripath ServletContext object represented by

2.getInitParameter (String name): return ServletConfig object name parameter

3.getMineType (String file): Back to file arguments on behalf of the file MIME type

4.getRequestDispatcher (String path): return path object on behalf of the RequestDispacher

5.getResourceAsStream (String path): return the form to enter the flow path corresponding to the resources, leaving the object in the input can be any form of data, path parameter must be "/" relative to the beginning and the Context Root

12, how to use ServletContxt read and save the properties file:

1. Create java.util.Properties class object

2. Get ServletContext object

3. The properties file to read input stream to form an input stream object

4. Will enter the stream object is loaded into the Properties object

5. The Properties object saved to the ServletContext object

13, How to load Web application startup static text:

1. Create inherited subclass of HttpServlet class, configure the Servlet in web.xml when the set load-on-startup attribute:

someclass

somepackage.SomeClass1


2. In the Servlet's init () method to create a java.util.Properties class

3. Get the current Web application's ServletContext object

4. The WEB-INF directory of the properties file read into the input stream InputStream in:

InputStream in = context.getResourceAsString ("WEB-INF/someproperties.properties");

5. The input stream is loaded into the property object

ps.load (in);

6. The property of the object saved to the context.

context.setAttribute ("attributeName", ps);

14, how to create a label deal with categories:

1. The introduction of the necessary resources:

import javax.servlet.jsp .*;

import javax.servlet.http .*;

import java.util .*;

import java.io. *;

2. Inherited TagSupport class and override doStartTag () / doEndTag () method

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

4. Get the key from the Properties object attribute values corresponding

5. On access to the property address and corresponding output

15, create a tag library description file (Tag Library Descriptor):

1. Tag library description file, referred to as TLD, using XML file format, define the user's tag library. TLD file elements can be divided into three categories:

A.: tag library elements

B.: label element

C.: attribute element

2. Tag library element is used to set the tag library of related information, it's common attributes:

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

B.uri: Set Tag Library only access to that character

3. Label element is used to define a label, it's common attributes:

A.name: Set Tag name

B.tagclass: to set the processing type Tag

C.bodycontent: Settings tab of the main (body) content

1). Empty: that there is no body tag

2). JSP: body that labels can be added JSP code

3). Tagdependent: that label content from the labels themselves to deal with

4. Tag attribute element is used to define tag attributes, and its common attributes:

A.name: attribute name

B.required: attribute is required, default is false

C.rtexprvalue: property value if it is possible request-time expression, which is similar to the expression

16, in Web applications using the tag:

1. If the Web application uses a custom JSP tag, you must add elements in the web.xml file, it cited for the statement where the label tag library

/ Sometaglib

/ WEB-INF/someTLD.tld

2.: Set Tag Library's unique identifier, in the Web application will be referenced according to its Tag Libray

3.: Tag Library specified and the location of the corresponding TLD file

4. In the JSP file needs to join

5.prefix in JSP pages that reference the tag library tag prefix, uri identifier used to specify the Tag Library, it must be consistent with web.xml in the property.







Recommended links:



Town and only "limited EARNINGS"



M2TS Converter



Guide to fully experience the still-mail (a) - attachment



MPG To 3GP



Guide Games Sports



Alliance with the merger as



Alliance with The merger as



Hot stocks head Invasion: with input for the stock market frenzy



Introduction Automation Tools



ASF to MOV



Stop Mmc Exe Problems Repair It Now



Dial Up And Connection Tools Expert



HE New Year! Colorful fireworks animation production



Capital GRAY screen under the Waterfront



No comments:

Post a Comment