Scope of JSP variables in specifications

Source: Internet
Author: User
Tags tld

The assumerver Pages (JSP) and JSP standard tag Library (JSTL) provide many useful tags for Web developers, also known as operations ). In addition, JSP 2.0 provides two APIs: Standard tag API and simple tag API, used to build custom tag/operation. The previous API is inherited from JSP 1.xand used by JSTL for historical reasons. Since JSTL 1.0 was developed before JSP 2.0, the new API does not contain JSTL 1.1 .) In addition, JSTL does not use new JSP features such as JSP fragments and dynamic attributes. This article uses new APIs and features of JSP 2.0 to build custom tag extension JSTL. This article provides an API overview and demonstrates how to develop
◆ Export variable tag
◆ Condition mark
◆ Iteration mark
◆ Tags with dynamic attributes
◆ Coordination mark

Overview of simple tagging APIs

When custom tags are used on the JSP page, the JSP Container of the Application Server ... Convert to the Java code that calls a method called a tag processing class. Therefore, to develop custom tags, you must provide a tag processing class, which must use the JSP 1.x standard tag API or JSP 2.0 simple tag API. By comparing these two APIs, you will find that the new APIs are easier to use. The simple tag API has only one interface (javax. servlet. jsp. tagext. SimpleTag), which defines the methods for processing custom tags. These methods are usually called from the Java Servlet automatically generated by the JSP Container from the JSP page.

The javax. servlet. jsp. tagext. SimpleTagSupport class implements the SimpleTag interface. Therefore, when the tag processing class extends SimpleTagSupport, you only need to write the doTag () method. The following steps describe how to develop a simple tag processing class:

Step 2: design custom tags
First, you must select a name for the tag and set its attributes. Then, create a tag library Descriptor (TLD) file that uses the XML format defined by the JSP Specification) to inform the JSP Container of how to process and verify the custom tag. The text provides a sample tld file named util. TLD.

Step 2: Create a tag processing class
A Java class must be provided to implement the SimpleTag interface. The simplest way is to extend SimpleTagSupport or a subclass of SimpleTagSupport. The VarTagSupport, IfTag, and WhileTag classes in this article are used to extend SimpleTagSupport. Added VarTagSupport for other tag processing examples.

To use attributes not specified in the TLD file, the tag processing class must implement javax. servlet. jsp. tagext. the DynamicAttributes interface is shown in the MapTag example described in the "mark with dynamic attributes" section ).

Step 2: Initialize the tag processing class instance
Each tag processing class must contain a public constructor without parameters to place the initialization code. Some tag processing classes in this article include EvalTag, ListTag, and MapTag. They contain a public constructor without parameters, which uses the default value to initialize instance variables. Other IfTag, WhileTag, and ItemTag classes are not constructors. Note that the Java compiler automatically generates a public constructor without any constructor In the case that the class does not contain any constructor. This function does not perform any operations.

Step 2: Set attributes
The tag attribute values on the JSP page are passed to the tag processing class through the setAttribute () method. For example The tag contains four attributes: var, scope, expr, and type. The EvalTag processing class implements the setExpr () and setType () methods, and inherits setVar () and setScope () from VarTagSupport ().

Dynamic attributes are passed through the setDynamicAttribute () method defined by the DynamicAttributes interface.

Step 2: implement the doTag () method
This method is used to implement custom tag logic. The doTag () method is called by the JSP Container after all the attribute setting methods. You can use getJspContext () to obtain a javax. servlet. jsp. JspContext object to access the JSP environment. You can call getJspBody () to return the javax. servlet. jsp. tagext. JspFragment instance. And . If you want to develop tags for collaborative work, such And You can also use the getParent () and findAncestorWithClass () methods.

Step 2: Test the custom tag
The custom tag JSP page must use the <% @ taglib %> command to import the tag library. When the custom tag appears on the JSP page, the JSP Container generates code for creating the tag processing class instance, calling the Property setting method, and calling the doTag () method. Therefore, the tag processing method is called during execution of JSP pages with custom tags.

Restrictions and workarounds

To simplify the tag processing API, JSP 2.0 imposes a restriction: If the custom tag processing class is based on the simple tag API, the page author cannot And Use JSP 1.x Declaration (<% !... %>), JSP 1.x expressions (<% =... %>), and scriptlet (<%... %> ). In most cases, you can move the Java code on the JSP page to the tag processing class, or in the JSP 2.0 expression ($ {...}) JSTL can be used in custom labeled subjects. Note that JSP 2.0 allows you to use scriptlet in a custom tag entity based on the standard tag API. However, since JSP pages without scripts are easier to maintain, it is best to avoid using Java code in Web pages.

My previous Oracle Technology Network (OTN) article "Using JSP 2.0 el api" introduced another restriction for simple marking of APIs and provided workarounds. The JspContext class does not provide access to JSP implicit objects such as application, session, request, and response. Most Application servers, including Oracle Application Server Containers for J2EE (Solaris) 10 Gb, allow conversion of JSP context to PageContext

The tag processing class is not applicable to generating a large number of reusable HTML code using println () statements. JSP 2.0 provides a better method for this work. The so-called Tag file uses JSP syntax and is automatically converted from a JSP Container to a tag processing class based on simple tag API. My other OTN article "Create a JSP 2.0 Tag file" introduces this new JSP feature.

Export variable tag

Many JSTL tags implement a logic and export JSP variables to report results. For example, Contains a var attribute, which must specify the name of the JSP variable used to save the SQL result set. Mark other JSTL attributes as shown in And Is optional. If the var attribute does not exist, these tags will output their results. All tags that contain the var attribute also contain a scope attribute, which can be used to indicate the scope of the following JSP variables: page, request, session, or application.

VarTagSupport class, which is an example developed in this article) extends SimpleTagSupport and provides setting methods for var AND scope attributes. VarTagSupport includes a practical method used to export JSP variables, obtain the subject content, and output content, rather than implementing the doTag () method. These methods are used by the subclass of VarTagSupport in doTag. This document contains four tag processing classes for extended VarTagSupport: EvalTag, MapTag, ListTag, and ItemTag ).

Note that JSP variables are called range variables in the JSTL specification, while JSP variables are called named variables or range attributes. These variables are created/exported using the setAttribute () method of the JspContext class. You can use $ {varName} On the JSP page and the getAttribute () or findAttribute () method of JspContext in Java code to get their values. Do not confuse JSP variables with tag attributes.

  1. Brief Introduction to JSP Technology
  2. Ten JSP tag libraries to be mastered
  3. Common Methods of form data storage in JSP Technology
  4. Detailed explanation of JSP technology methods
  5. How to Learn JSP Technology

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.