Standard JSP tags can call the JavaBeans component or execute customer requests, which greatly reduces the complexity and maintenance of JSP development. JSP technology also allows you to customize taglib. In other words, taglib can be seen as an extension of JSP tags, just as XML is an extension of HTML. Generally, taglib is defined in the tag library. The tag inventory contains your own tag tags. In short, if you use taglib, you can design your own JSP tag!
In general, custom tag tags are mainly used to operate hidden objects, process HTML submission forms, access databases, or other enterprise-level services, such as mail and directory operations. Users of custom tag tags are generally programmers who are very familiar with the Java programming language and are familiar with data access and enterprise-level service access. For HTML designers, this allows him to focus on web design without focusing on complicated business logic. At the same time, it also rationally divides database developers and database users, and custom tag tags encapsulate repetitive work, which greatly improves productivity, in addition, the tag library can be used in different projects, which perfectly reflects the idea of software reuse.
In this article, we mainly discuss:
· What is a custom tag?
· How to Use tag tags?
O declare the tag library to be used
O find the corresponding tag processing class
O tag type
· Custom tag
O tag processing class
O tag library description
O tag example
O tag with attributes
O tag with body
O defines the tag of the script variable
O tags with collaboration relationships
· Custom tag
O an iteration tag example
O A template tag Library
O how is the tag processing class called?
What is a custom tag?
A custom tag is a type of JSP tag defined by the user. When a JSP page containing custom tag tags is compiled into a servlet by the JSP Engine, tag tags are converted into operations on an object called Tag processing class. Therefore, when the JSP page is converted into a servlet by the JSP engine, the tag is actually converted into a tag processing operation.
Custom tag tags have many characteristics, such:
· You can customize tag attributes on the JSP page.
· Access all objects on the JSP page
· Page output can be dynamically modified
· Communication with each other. You can create a JavaBeans component, call the JavaBeans component in one tag, and call it in another tag.
· Tags can be nested with each other. You can perform complex interactions on a JSP page.
Use tag tags
This section describes how to use tag tags on JSP pages and the types of tag tags.
To use tag tags, JSP programmers must do two things:
· Tag library for declaring this tag
· Implement this tag
The tag library where the tag is declared
If you want to use the tag, use the taglib indicator of JSP to specify its tag Library (Note: taglib should be declared before using this tag)
<% @ Taglib uri = "/WEB-INF/tutorial-template.tld" prefix = "TT" %>
The URI attribute defines the unique tag library description (TLD). It can be a TLD file name or a unique name. Prefix is a method used to distinguish tags with duplicate names in other TLD and local TLD.
The TLD must use. TLD as the extension and be stored in the WEB-INF directory or its subdirectory of the current application. You can directly reference it through its file name, or indirectly reference it in other ways.
The following taglib indicator directly references a TLD:
<% @ Taglib uri = "/WEB-INF/tutorial-template.tld" prefix = "TT" %>
The following taglib indicator indirectly references a TLD using a logical Name:
<% @ Taglib uri = "/tutorial-template" prefix = "TT" %>
If the TLD is indirectly referenced, you must go to the Web. define the ing between the logical name and the TLD file in XML. add an element named taglib to XML:
<Taglib>
<Taglib-Uri>/tutorial-template </taglib-Uri>
<Taglib-location>
/WEB-INF/tutorial-template.tld
</Taglib-location>
</Taglib>
Implement this tag
To implement tag tags, you have two methods to store tag processing classes. I. Set the tag processing class. class stored in the current application of the WEB-INF/class subdirectory, 2, if the tag processing class is in the form of a jar package, it can be placed in the current application of the WEB-INF/lib directory, if the tag processing class needs to be shared among multiple applications, it should be placed under the Common/lib directory on the JSP server. For tomcat, it is under the tomcat/common/lib directory.
Tag tag type
Custom tag tags follow the XML syntax. It has a start mark and an end mark, and some have a body (that is, a text node ):
<TT: Tag>
Body
</TT: Tag>
A tag without body is as follows:
<TT: tag/>
Simple tag
A tag without body and attributes is as follows:
<TT: simple/>
Tag tags with attributes
Custom tags can have their own attributes. Attributes are generally defined in the start tag. The syntax is ATTR = "value ". The role of an attribute is equivalent to a parameter of a custom tag, which affects the behavior of the tag processing class. You can define it in the TLD.
You can use a String constant to assign values to an attribute, or assign values to it through an expression, such as <% =... %>. Taking struts as an example, its logic: Present tag is a String constant used to assign values to attributes:
<Loglic: Present parameter = "clear">
Another label, logic: iterate, is used to assign values to attributes:
<Logci: iterate collection = "<% = bookdb. getbooks () %>"
Id = "book" type = "database. bookdetails">
Tag with body
A custom tag can contain other custom tags, script variables, HTML tags, or other content.
In the following example, the logic: Present label of Struts is used on this JSP page. If some labels define the property of parameter = "clear", the content of the shopping cart is cleared, then a message is printed:
<Logic: Present parameter = "clear">
<% Cart. Clear (); %>
<Font color = "# ff0000" size = "+ 2"> <strong>
You have selected to clear the shopping cart!
</Strong> </font>
</Logic: Present>
Whether to use attributes or body to transmit information?
As mentioned above, we can pass information either through attributes or through body. But in general, it is better to use attributes to transmit information for simple types, such as strings or simple expressions.
Define tag tags for script variables
The so-called script variable refers to the variables or objects that can be called in JSP. It can be generated by TAG tags. The following example illustrates how a tag defines a transaction processing object defined by JNDI named Tx. Script variables can be EJB objects, transactions, database connections, and so on:
<TT: Lookup id = "TX" type = "usertransaction" name = "Java: COMP/usertransaction"/>
<% Tx. Begin (); %>
...
Tag tags with collaboration relationships
You can use shared objects to collaborate between custom tag tags. In the following example, tag1 creates an object named obj1, and OBJ can still be reused in tag2.
<TT: tag1 attr1 = "obj1" value1 = "value"/>
<TT: tag2 attr1 = "obj1"/>
In the following example, if the tag of the outer layer creates an object, all tag tags of the outer layer can use this object. Because the generated object does not have a specified name, the conflict of duplicate names can be reduced. This example illustrates a series of collaborative nested objects.
<TT: outertag>
<TT: innertag/>
</TT: outertag>
Tag processing class
The tag processing class must implement the tag interface or bodytag interface. However, it is common to inherit from the tagsupport or bodytagsupport class. These classes or interfaces can be stored in javax. servlet. JSP. in the tagext package.
When the JSP Engine sees that its JSP page contains TAG tags, it will call the dostarttag method to process the beginning of the tag, and call the doendtag method to process the end Of the tag.
The following table describes the different processing processes required for different types of tags:
Tag Processing Method
Tag tag type
Method called
Basic tag
Dostarttag, doendtag, release
Tags with attributes
Dostarttag, doendtag, set/getattribute1. .. n, release
Tag with content
Dostarttag, doendtag, release
Tags with content and repeated content Loops
Dostarttag, doafterbody, doendtag, release
Tags with content and content interaction with JSP
Dostarttag, doendtag, release, doinitbody, doafterbody, release
A tag processing class can use javax. servlet. JSP. pagecontext to interact with JSP, through javax. servlet. JSP. pagecontext class. The tag processing class can access the request, session, and application images in JSP.
If tag tags are nested with each other, the tag processing class in the inner layer can access the tag processing class at the upper layer through its parent attribute.
Generally, all tag processing classes are packaged as jar packages for release.
Tag library description (TLD)
The tag library is described in XML. TLD includes the description of all tag tags in the tag library. It is generally used by the JSP server to verify the syntax correctness of the tag, or JSP developers can use it to develop new tags.
The TLD file extension must be. TLD and must be placed in the WEB-INF directory or its subdirectory of the current web application.
The content of a TLD must start with the standard XML to describe the DTD and XML versions. For example:
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.2 //" http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd ">
TLD must use <taglib> as its root element. The sub-elements of <taglib> are as follows:
Child element of <taglib>
Element
Description
Tlib-version
Version of the tag Library
JSP-version
JSP version required by the tag Library
Short-name
Mnemonic, an alias of a tag (optional)
Uri
Used to determine a unique tag Library
Display-name
Name used by visualization tools (such as JBuilder) (optional)
Small-icon
Small icons used by visualization tools (such as JBuilder) (optional)
Large-icon
Large icons used by visualization tools (such as JBuilder) (optional)
Description
Description of the tag Library (optional)
Listener
See the following listener element.
Tag
See the Tag Element below
Listener Element
A tag library may define some classes as its event Listening Classes. These classes are called listener elements in a TLD, And the JSP server will instantiate these Listening Classes and register them. The listener element contains a child element called listener-class. The value of this element must be the complete Class Name of the listener class.
Tag Element
Each Tag Element must indicate its name, class name, script variable, and tag attribute in the tag library. The value of the script variable can be directly defined in the TLD or obtained through the tag-attached information class. Each attribute describes whether this attribute can be omitted and whether its value can pass <% =... %> JSP syntax and attribute type.
Each tag corresponds to a Tag Element in the TLD. The following table lists the sub-elements of the tag element:
Child element of the Tag Element
Element name
Description
Name
Unique element name
Tag-class
Tag processing class corresponding to tag
Tei-class
Subclass of javax. servlet. jsp. tagext. tagextrainfo, used to express script variables (optional)
Body-content
Type of the tag TAG body
Display-name
Name used by visualization tools (such as JBuilder) (optional)
Small-icon
Small icons used by visualization tools (such as JBuilder) (optional)
Large-icon
Large icons used by visualization tools (such as JBuilder) (optional)
Description
Description of the tag
Variable
Provide script variable information (same as Tei-class) (optional)
Attribute
Attribute name of the tag
The following sections describe how to implement tags of different types.
Simple tag
Tag processing class
A simple tag processing class must implement the dostarttag and doendtag methods of the tag interface. When the JSP Engine starts with a tag, the dostarttag is called. Because a simple tag does not have a body, this method returns the skip_body. When the JSP Engine encounters the end Of the tag, the doendtag is called. If the remaining page is still computed, it returns eval_page; otherwise, it returns skip_page.
The following is an example: For a tag <TT: simple/>, its tag processing class is implemented as follows:
Public simpletag extends tagsupport
{
Public int dostarttag () throws jspexception
{
Try {
Pagecontext. getout (). Print ("hello .");
} Catch (exception e ){
Throw new jsptagexception ("simpletag:" + E. getmessage ());
}
Return skip_body;
}
Public int doendtag ()
{
Return eval_page;
}
}
Note: If the tag has no content, the body-content element must be defined as null. For example:
<Body-content> Empty </body-content>
Tag tags with attributes
Tag processing class
For each attribute of a tag, you must define its attributes according to the JavaBeans specification, as well as get and set methods. Take the logic: Present label of struts as an example,
<Logic: Present parameter = "clear">
Correspondingly, the tag processing class should have the following methods and definitions:
Protected string parameter = NULL;
Public String getparameter ()
{
Return this. parameter;
}
Public void setparameter (string parameter)
{
This. Parameter = parameter;
}
NOTE: If your attribute name is ID and your tag processing class is inherited from the tagsupport class, you do not need to define its attributes and the set and get methods, because they have already been defined in tagsupport.
Attribute Element
For each attribute of a tag, you must define whether it is required and whether its value can be <% =... And its type (optional). If you do not specify its type, the default value is Java. Lang. String. If the rtexprvalue element is defined as true or yes, the attribute return type is defined in the type element.
<Attribute>
<Name> attr1 </Name>
<Required> true