JavaServer Pages (JSP) and JSP Standard Tag Library specification (JSTL) to Web developers to provide many useful tags (also known as operations). In addition, JSP 2.0 also provides two API, which is the standard API and simple tags tags API, used to build custom tags / operation. Inherited from the previous API JSP 1.x, and due to historical reasons, the use by the JSTL. (Due to the development of JSTL 1.0 in JSP 2.0 before, so the new API does not include JSTL 1.1.) Moreover, JSTL does not use JSP fragment and dynamic properties of the new features such as JSP. This article uses the JSP 2.0 API and features of the new build custom tag extensions JSTL. This article provides an overview of API and demonstrates how to develop
鈼?Export variable tag
鈼?conditions of tags
鈼?iteration mark
鈼?dynamic properties of the tag
鈼?Coordination tag
Simple tag API Overview
In JSP pages using custom tags, the application server JSP container will convert the call ... as tag processing methods like Java code. Therefore, if you want to develop custom tag, you must provide a tag handler, such standards must be marked using JSP 1.x API or the JSP 2.0 simple tag API. Compare these two API, will find the new API easier to use. Only a simple tag API Interface (javax.servlet.jsp.tagext.SimpleTag), which defines the method of dealing with custom tags. Usually from the JSP container automatically generated from the JSP page in the Java Servlet to call these methods.
javax.servlet.jsp.tagext.SimpleTagSupport class implements SimpleTag interface, so when the mark when dealing with only the preparation of an Extended SimpleTagSupport doTag () method can be. The following steps describe how to develop a simple tag handler class:
Step 1: Design custom tag
First, you must choose a name tag and set its properties. Then, create a tag library descriptor (TLD) file (using the JSP specification defines the XML format) to inform the JSP container how to handle and verify the custom tag. Text provides a sample TLD file called util.tld.
Step 2: Create tag handling class
Must provide an interface for achieve SimpleTag Java classes. The easiest way is to extend SimpleTagSupport or one of its subclasses. In this article VarTagSupport, IfTag and WhileTag class for extending SimpleTagSupport. Examples of other tag handler extension VarTagSupport.
If you want to use is not specified in the TLD file attributes, then the tag handler must implement the javax.servlet.jsp.tagext.DynamicAttributes interface (such as "dynamic attributes of the tag" section describes the MapTag example below).
Step 3: Initialize tag handle class instance
Each tag handling class must contain a public no arguments constructor for placing the initialization code. In this article address some of the tag type (EvalTag, ListTag and MapTag) contains a public constructor without parameters, it uses the default values instance variables initialized. Other (IfTag, WhileTag and ItemTag) no constructor. Please note, Java compiler in the class does not contain any constructors to automatically generate a public constructor without parameters, this function does not do anything.
Step 4: Method of providing property
JSP page tag attribute values through the setAttribute () method is passed to the tag handler. For example, this article includes four tag attributes: var, scope, expr and type. EvalTag handling class implements setExpr () and setType () method, inherited from the VarTagSupport setVar () and setScope ().
Dynamic properties of interfaces defined by DynamicAttributes setDynamicAttribute () method of delivery.
Step 5: Implement doTag () method
The method used to implement custom tag logic. doTag () method by the JSP container, after following all the property set method call. Here you can use getJspContext () to get a javax.servlet.jsp.JspContext object to access the JSP environment. Can call getJspBody (), which returns javax.servlet.jsp.tagext.JspFragment instance, the instance that is located in the main body and between the JSP. If you work together to develop the tags, such as and (the last part of this article will be introducing its), you can also use the getParent () and findAncestorWithClass () method.
Step 6: Test the custom tag
JSP pages using custom tags to use the tag directive to import the tag library. When the custom tag in JSP page, when, JSP container generates the class instance to create tag handling, call the property method and call doTag () method code. Therefore, the use of JSP custom tag implementation of the page call tag will handle class methods.
Limitations and workarounds
To simplify the tag processing API, JSP 2.0 adopted a limit: If the custom tag processing class is based on the simple tag API, then the page author and shall not be used between the JSP 1.x statement (), JSP 1.x expression ( ) and scriptlet (). In most cases, you can Java code in JSP page to move to tag handling class, or in JSP 2.0 Expression (${...})( can use the custom tag's body) to use JSTL. Please note, JSP 2.0 allows you to standards-based API for custom tag to use the body tag scriptlet. However, because the script does not use JSP pages easier to maintain, it is best to avoid the use of Java in the Web page code.
My previous Oracle Technology Network (OTN) article, "Using JSP 2.0 EL API" describes a simple tag API, and provides another limit workaround. JspContext class is not available on the JSP implicit objects (such as application, session, request and response) of the visit. Most application servers (including Oracle Application Server Containers for J2EE (OC4J) 10g) allows to convert the JSP PageContext context
Tag handler does not apply to the use println () statement generates a large number of reusable HTML code. JSP 2.0 this work provides a better way. JSP tag files to use the so-called by the JSP container syntax is automatically converted to a simple tag on the tag handler API. Another one of my OTN article "Creating JSP 2.0 tag files", introduced new features to this JSP.
Export variable tag
Many JSTL tags to achieve a logical and export JSP variable to report the results. For example, contains a var attribute must specify the SQL result set to save the JSP variable name. var property on the other JSTL tags (such as and) is optional. If the var attribute does not exist, then these tags will output their results. Var properties all contain the tags also contain a scope attribute that can be used to indicate the following JSP variable scope: page, request, session or application.
VarTagSupport class (which is an example of this development) Extension SimpleTagSupport var and scope attributes for the provision of setting method. VarTagSupport contains export JSP variables, access to the main content and practical method of output, rather than to achieve doTag () method. These methods from the sub-class VarTagSupport doTag () to use. This article includes four tag for the expansion VarTagSupport handling class (EvalTag, MapTag, ListTag and ItemTag).
Please note, JSP variable in JSTL specification called the scope of variables in the JSP specification, called the scope of named variables or attributes. These variables JspContext class by setAttribute () method to create / export. You can use the JSP page $ (varName), and the use of Java code JspContext the getAttribute () or findAttribute () method to obtain their values. Do not confuse the JSP tag variables and attributes.
相关链接:
Easy Graphic
ASF to AVI
Symantec said the new storm worm detected
"Chino conflict" Comment
DV to AVI
LIVEMOTION make the drop-down menu
MP4 to FLV
2007 College Graduates in Beijing reached 200 000
Restore usb flash drive
Cisco Crusade battle with Hewlett-Packard launched in China
PIMS And Calendars Infomation
DESKTOP Directory
Trust By The Bank Through The East, TongLINK / Q Acclaimed
Easy E-Mail Clients
Summer Cold! Photoshop To Add Some Ice Cup
Sogou Pinyin input method and How to do conflict game?