JSP tag files (tag file)
Foreword: Before we introduced the custom label, through writes the script-free JSP file, may promote the division of labor, the page designer can work with the background logic coder simultaneously. However, writing custom labels is too lengthy and trivial. Starting with JSP2.0, the tag file allows you to customize tags without having to write label processing classes and tag profiles.
I. Introduction of TAG File
Tag file simplifies the development of custom tags from two aspects. First, the tag file does not need to be compiled in advance until it is called for the first time. In addition, the extended definition of tags can be done using only JSP syntax.
Second, the tag library description file is no longer needed.
The JSP container provides several ways to compile the tag file into a Java label processing class. For example, Tomcat translates the tag file into a label processing class that inherits from the Javax.servlet.jsp.tagext.SimpleTag interface.
A tag file, like a JSP page, has instructions, scripts, El expressions, action elements, and custom tags. A tag file is suffixed with tag or TAGX, which can contain other resource files. A tag file that is contained by another file should be suffixed with TAGF.
There are also hidden objects in tag file that can be accessed through a script or an El expression. These objects are similar to hidden objects in JSPs.
Object |
Type |
Request |
Javax.servlet.http.HttpServletRequest |
Response |
Javax.servlet.http.HttpServletResponse |
Out |
Javax.servlet.jsp.JspWriter |
Session |
Javax.servlet.http.HttpSession |
Appliction |
Javax.servlet.ServletContext |
Config |
Javax.servlet.ServletConfig |
Jspcontext |
Javax.servlet.jsp.JspContext |
Second, the first tag file
The following example contains a tag file and a JSP page that uses this tag file. The name of this tag file is Firsttag.tag and the code listing is as follows:
Firsttag.tag
<%@ Tag Import="java.util.Date"Import="Java.text.DateFormat"%><%DateFormat DateFormat=dateformat.getdateinstance (Dateformat.long); Date Now = New Date(System.currenttimemillis ()); Out.print (Dateformat.format ( Now));%>
firsttagtest.jsp page
<% @ taglib prefix = " easy " Tagdir = " /web-inf/tags " %> today is < easy:firsttag />
The effect is as follows:
Third, tag file directive
Like JSP pages, tag file can use instructions to command how the JSP container compiles the tag file.
In addition to the page directive, all other JSP directives can be used for tag file. In tag file, you can use the tag command instead of the page directive. In addition, you can also use two new commands: attribute and variable.
Tag file directive
Instructions |
Describe |
Tag |
Functions similar to page directives in JSP pages |
Include |
Used to import additional resources into the tag file |
Taglib |
Used to import custom tag libraries into the tag file |
Attribute |
Used to import custom tag libraries into the tag file |
Variable |
Used to import custom tag libraries into the tag file |
1. Tag Instructions
The tag directive is similar to the page directive in the JSP. The following is the property sheet.
Properties |
description |
display-name |
in XML tool The name displayed in the. The default is a tag file that does not contain a suffix |
body-content |
Specifies the type of the label body, the Body-content property value is empty, tagdependent, SCRI Ptless, the default value is scriptless |
dynamic-attributes |
Specifies the name of the tag file dynamic property. When the Dynamiattributes value is set, a map is generated to hold the names of these dynamic properties and the corresponding values |
small-icon |
Specifies a picture path that is used to display XML Show Small Icons |
large-icon |
Specify a picture path for displaying large icons on XML |
Description |
description Information for tags |
Example |
tags use the description of the instance |
Language |
The script that is used in the tag file for your language type. Must be set to "Java" |
import |
is used to import a Java type that is identical to the import in a JSP page |
pa Geencoding |
To specify the encoding format used by the tag file |
Except for the Import property, all other properties can appear only once in a tag designation or in a tag file.
2. Include directives
The include directive in the tag file is the same as the include in the JSP page.
3. Taglib Instructions
You can use the custom label in tag Lib with the Taglib directive. The syntax of the TAGLIB directive is as follows:
<uri= "Taglibraryuri" prefix= "tagPrefix" %>
4. Attribute Instructions
The attribute is used to set the properties of the tag in the tag file. It is equivalent to the attribute element in the tag library description file.
The properties of the ATTRIBUTR directive are as follows, where only the name attribute is required.
Property |
Describe |
Name |
Used to set the name of the property. In a tag file, the name of each property is unique |
Required |
Used to set whether this property is required. Value can take true and false |
Fragment |
Used to set whether the property is fragment. |
Rtexprvalue |
Used to set whether the value of this property is dynamically computed at run time |
Type |
The type used to set the property |
Description |
Used to set descriptive information for this property |
5. Variable Instructions
Some of the values in the tag file can be passed to the JSP page via variable. The syntax of the variable directive is as follows:
<(attribute= "value") *%>
The properties of the variable directive are described in the following table:
Property |
Describe |
Name-given |
The variable name. |
Name-from-attribute |
Similar to the Name-given property, the name of the variable is determined by the value of the Label property. An error occurs if both properties appear or do not appear |
Alias |
Set a local range to receive the value of the variable |
Variable-class |
Types of variables |
Declare |
Sets whether the variable declares |
Scope |
Used to specify the range of the variable |
Description |
Used to describe the variable |
Compared to JspWriter: JspWriter can simply pass a string to a JSP page with poor flexibility.
Iv. Dobody
The Dobody action element can only be used in tag file, and it is used to invoke the ontology content of a tag.
Dobody action elements can also have attributes. You can use these properties to specify a variable to receive the subject content, and if these instructions are not applicable, then the Dobody action will write the main content to the JspWriter of the JSP page.
The properties of the Dobody action element are as follows:
Property |
Describe |
Var |
The value of the variable that holds the contents of the label body, which is stored in the variable as the type of string. var and varreader can only show one |
Varreader |
The value of the variable that holds the contents of the tag body, which is stored in the variable as the type of reader. |
Scope |
Scope of variable Save |
V. Invoke
The invoke action element is similar to Dobody, which can be used to invoke a fragment in the tag file. Properties of the Invoke action element:
Property |
Describe |
Fragment |
The name of the fragment to invoke |
Var |
The value of the variable that holds the content of the fragment body, and the subject content is stored in the variable as string, and the Var and Varreader properties can only appear in one |
Varreader |
The value of the variable that holds the content of the fragment body, which is stored in the variable with the reader type, and the Var and Varreader properties can only appear in one |
Scope |
Scope to which the variable is saved |
------Days if the day is also old, the human path is the vicissitudes of the world------
Javaweb Learning--tag file