This article discusses tag files ). We have discussed the development of traditional tags and simple tags in servlet & JSP (16) and Servlet & JSP (16, you need to use the Java language to compile the tag processor class. The Tag file allows the JSP page writers to customize the tag using the JSP syntax. You do not need to understand the Java language. This article describes how to customize tags by using tag files. If they can be combined with the previous two articles, they will better understand the Tag file.
Tag File Syntax
Its syntax is similar to JSP syntax. The syntax that can be used in JSP pages can be used in tag files, but there are differences.
1) The page command on the JSP page cannot be used in the Tag file. The tag command, attribute command, and variable command are added to the Tag file.
2) The <JSP: invoke> and <JSP: Dobody> standard action elements can only be used in tag files.
Let's first look at a simple tag file hello. Tag. As follows:
<% @ Tag pageencoding = "gb2312" %> This Is A Tag file.
Tag commands can only be used in tag files. They are used to replace the page commands on JSP pages.
To use the compiled Tag file on the JSP page, you must store it in the specified location. There are two places: one is put in the/WEB-INF/tags directory or its subdirectory, the container will automatically search for the. Tag and. tagx files. The second is put in the/META-INF/tags directory of the JAR file or its subdirectory.
Now, compile a JSP page to test it. As follows:
<%@ page contentType="text/html;charset=gb2312" %><%@taglib tagdir="/WEB-INF/tags" prefix="mytag"%>
Note: In the taglib command, the attribute tagdir specifies the path of the Tag file.
You can start Tomcat to access this page. Enter http: // localhost: 8080/testtag/Hello. jsp in the browser URL bar. The running result is as follows:
We can find a webhello_tag.java file in the D: \ apache-Tomcat-7.0.33 \ work \ Catalina \ localhost \ testtag \ org \ apache \ JSP \ tag folder. The content is as follows:
/** Generated by the Jasper component of Apache Tomcat * version: Apache Tomcat/7.0.33 * generated at: 2012-12-14 08:32:17 UTC * Note: the last modified time of this file was set to * The Last Modified Time of the source file after * generation to assist with modification tracking. */package Org. apache. JSP. tag; import javax. servlet. *; import javax. servlet. HTTP. *; import javax. servlet. JSP. *; public final class webhello_tag extends javax. servlet. JSP. tagext. simpletagsupport implements Org. apache. jasper. runtime. jspsourcedependent {Private Static final javax. servlet. JSP. jspfactory _ jspxfactory = javax. servlet. JSP. jspfactory. getdefafactory Factory (); Private Static Java. util. map <Java. lang. string, Java. lang. long> _ jspx_dependants; private javax. servlet. JSP. jspcontext; private Java. io. writer _ jspx_sout; private javax. el. expressionfactory _ el_expressionfactory; private Org. apache. tomcat. instancemanager _ jsp_instancemanager; Public void setjspcontext (javax. servlet. JSP. jspcontext CTX) {super. setjspcontext (CTX); Java. util. arraylist _ jspx_nested = NULL; Java. util. arraylist _ jspx_at_begin = NULL; Java. util. arraylist _ jspx_at_end = NULL; this. jspcontext = new Org. apache. jasper. runtime. jspcontextwrapper (CTX, _ jspx_nested, _ jspx_at_begin, _ jspx_at_end, null);} public javax. servlet. JSP. jspcontext getjspcontext () {return this. jspcontext;} public Java. util. map <Java. lang. string, Java. lang. long> getdependants () {return _ jspx_dependants;} private void _ jspinit (javax. servlet. servletconfig config) {_ el_expressionfactory = _ jspxfactory. getjspapplicationcontext (config. getservletcontext ()). getexpressionfactory (); _ jsp_instancemanager = org. apache. jasper. runtime. instancemanagerfactory. getinstancemanager (config);} public void _ jspdestroy () {} public void dotag () throws javax. servlet. JSP. jspexception, Java. io. ioexception {javax. servlet. JSP. pagecontext _ jspx_page_context = (javax. servlet. JSP. pagecontext) jspcontext; javax. servlet. HTTP. httpservletrequest request = (javax. servlet. HTTP. httpservletrequest) _ jspx_page_context.getrequest (); javax. servlet. HTTP. httpservletresponse response = (javax. servlet. HTTP. httpservletresponse) _ jspx_page_context.getresponse (); javax. servlet. HTTP. httpsession session = _ jspx_page_context.getsession (); javax. servlet. servletcontext application = _ jspx_page_context.getservletcontext (); javax. servlet. servletconfig Config = _ jspx_page_context.getservletconfig (); javax. servlet. JSP. jspwriter out = jspcontext. getout (); _ jspinit (config); jspcontext. getelcontext (). putcontext (javax. servlet. JSP. jspcontext. class, jspcontext); try {out. write ("\ r \ n"); out. write ("This Is A Tag file");} catch (Java. lang. throwable t) {If (T instanceof javax. servlet. JSP. skippageexception) Throw (javax. servlet. JSP. skippageexception) T; If (T instanceof Java. io. ioexception) Throw (Java. io. ioexception) T; If (T instanceof Java. lang. illegalstateexception) Throw (Java. lang. illegalstateexception) T; If (T instanceof javax. servlet. JSP. jspexception) Throw (javax. servlet. JSP. jspexception) T; throw new javax. servlet. JSP. jspexception (t);} finally {jspcontext. getelcontext (). putcontext (javax. servlet. JSP. jspcontext. class, super. getjspcontext (); (Org. apache. jasper. runtime. jspcontextwrapper) jspcontext ). syncendtagfile ();}}}
From the code, we can see that this class inherits from the javax. servlet. jsp. tagext. simpletagsupport class. That is to say, the Tag file is essentially a simple tag. Therefore, tag files and simple tags are essentially no different.
Hidden attributes of tag files
Hidden attributes can be used in the Tag file: request, response, jscontext, session, application, out, And config. The implicit object type of jspcontext is javax. servlet. JSP. jspcontext, which is used to manage all range variables, is equivalent to the pagecontext hidden object on the JSP page.
Tag file commands
The main commands that can be used in the Tag file are taglib, include, attribute, and variable.
<%@ tag display-name="" body-content="" dynamic-attributes="" small-icon="" large-icon="" description="" example=""language="" import="" pageEncoding="" isELIgnored="">
The tag command is like the page command of the JSP webpage, used to set the Tag file. Display-name indicates the name specified by <display-Name> in the graphic development tool. Body-content indicates three possible values: Empty, scriptless, tagdependent, and empty. Empty indicates that there is no subject content in the tag, and scriptlet indicates the subject content El and JSP action elements in the tag, but cannot be a JSP script element. tagdependent indicates that the subject content in the tag is handled by the tag itself, the default value is scriptless. Dynamic-attributes indicates the name of the dynamic attribute of the Tag file. When dynamic-
When attributes is set, a set object of map type is generated to store the attribute name and value; small_icon indicates a small icon in the relative path of the TLD specified by <small-Icon> In the graphical development tool. The size is 16x16; large-Icon indicates the large icon of the relative path of the TLD specified by <large-Icon> in the graphic development tool. The size is 32x32; description indicates the information related to the label file. example indicates to add more label instructions, including examples of tag applications; language, import, pageencoding,
The attributes of iselignored are the same as those of the page command.
<%@ attribute name="" required="" fragment="" rtexprvalue="" type="" description=""%>
This command is used to set the attributes of a custom tag. Here, "name" indicates the attribute name; "required" indicates whether it is necessary. The default value is "false"; and "rtexprvalue" indicates whether the attribute value can be a run-time expression. True indicates that the attribute can be specified dynamically, for example, <mytag: Read num = "$ {Param. num} "/>. If this parameter is set to false, you must use a static method to specify the attribute value. type indicates the type of this attribute. The default value is Java. lang. string; description describes information about this attribute.
<%@ variable name-given="" name-from-attribute="" alias="" variable-class="" declare="" scope="" desription="">
This command is used to set the variable of the Tag file. Name-given indicates that the variable name is directly specified; Name-from-attribute indicates that a property value of the custom tag is the variable name; alias indicates that a local range attribute is declared, used to receive the value of a variable. Variable-class indicates the Class Name of the variable. The default value is Java. lang. string; declare indicates whether the default value of this variable is declared to be true; Scope indicates the range of this variable, range: at_begin, at_end and nested; default value: Nested; description describes information about the variable.
<JSP: invoke> Action Element
This element outputs the execution result of the jspfragment type attribute to the jspwriter object or saves it to the specified range variable.
<jsp:invoke fragment="frag" var="resultString" varReader="resultReader" scope="scope" />
Frament is a required property and is used to specify the name of a property of the jspfragment type. VaR is an optional attribute and specifies the name of a range variable. Its type is string. This variable stores the execution results of the jspfragment object. Attribute VaR and varreader can only specify one of them. If neither of them is specified, the execution result of the jspfragment object is output to the current jspwriter object. Varreader saves the execution result of the jspfragment object to a variable of the reader type. Scope is used to specify the JSP range of the variable. The default value is page.
<JSP: Dobody> Action Element
This label is used to execute the label body (jspfragment object), output the result to the jspwriter object, or save it to the specified range variable.
Reprinted please indicate the source: http://blog.csdn.net/iAm333