JSP1.0 can be implemented by inheriting TagSupport or Bodytagsupport to implement a custom tag processing method.
JSP2.0 also supports a simpler way to customize the tag, which is to simply store the JSP code as a *.tag or *.tagx tag definition file. Tag and TAGX files support not only classic JSP code, various tag template codes, but also XML-style JSP instruction code.
By convention, tag and TAGX files need to be placed in the Web-inf/tags directory.
Here are some simple examples:
1. Simply Show time Time.tag
<%@ tag import= "java.util.*" import= "java.text.*"%> <% dateformat
df = dateformat.getdateinstance ( Dateformat.long);
Date d = new Date (System.currenttimemillis ());
Out.println (Df.format (d));
%>
<%@ taglib prefix= "util" tagdir= "/web-inf/tags"%>
2. Copy string how many times Repeater.tag
<%@ attribute Name= "Count" type= "Java.lang.Integer" required= "true"%>
<%@ attribute name= "value" type= " Java.lang.String "required=" true "%>
<%!
Private String Repeater (Integer count, String s) {
int n = count.intvalue ();
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < n; i++) {
sb.append (s);
}
return sb.tostring ();
}
%>
<%
out.println (Repeater (count, value));
%>
<%@ taglib prefix= "util" tagdir= "/web-inf/tags"%>
3. Find the province Lookup.tag
<%@ tag import= "java.util.*"%> <%@ attribute name= "CityName" required= "true"%> <%@
Variable name-given= "province"%> <%@ variable name-given= "Population" variable-class= "Java.lang.Integer"%>
<% if ("Toronto". Equals (CityName)) {Jspcontext.setattribute ("province", "Ontario");
Jspcontext.setattribute ("Population", new Integer (2553400));
else if ("Montreal". Equals (CityName)) {Jspcontext.setattribute ("province", "Quebec");
Jspcontext.setattribute ("Population", new Integer (2195800));
else {jspcontext.setattribute ("province", "Unknown");
Jspcontext.setattribute ("Population", new Integer (-1)); }%> <jsp:dobody/>
<%@ taglib prefix= "util" tagdir= "/web-inf/tags"%>
The above are all classic JSP code used, and the 3rd example is implemented using a different code:
* Use Tags:
<%@ tag import= "java.util.*"%> <%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/jstl/" Core "%> <%@ attribute name=" CityName "required=" true "%>" <%@ variable name-given= "province"%> <%@ Vari Able name-given= "Population"%> <c:choose> <c:when test= "cityname eq ' Toronto ' > <c:set var=" Provin Ce "value=" Ontario "/> <c:set var=" Population "value=" 2553400 "/> </c:when> <c:when test=" CityName
eq ' Montreal ' > <c:set var= "Province" value= "Quebec"/> "<c:set" var= "Population" value= "2195800"/> </c:when> <c:otherwise> <c:set var= "province" value= "Unknown"/> <c:set "var=" Val Ue= "-1"/> </c:otherwise> </c:choose>%> <jsp:dobody/>
<%@ taglib prefix= "util" tagdir= "/web-inf/tags"%> <%@ taglib prefix=
"C" uri= "http://java.sun.com/jsp/" Jstl/core "%>
* Use JSP directives, which usually generate XML-formatted files in this way
<?xml version= ' 1.0 '?> <jsp:root ' xmlns:jsp= http://java.sun.com/JSP/Page ' > <jsp: Directive.tag import= "java.util.*"/> <jsp:directive.taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core" "/> <jsp:directive.attribute name=" CityName "required=" true "/> <jsp:directive.variable" Province "/> <jsp:directive.variable name-given=" Population "/> <c:choose> <c:when test=" CityName eq
' Toronto ' > <c:set var= "Province" value= "Ontario"/> "<c:set" var= "Population" value= "2553400"/> </c:when> <c:when test= "cityname eq ' Montreal ' > <c:set var=" Province "value=" Quebec "/> < C:set var= "Population" value= "2195800"/> </c:when> <c:otherwise> <c:set var= "province" value= "Unknown"/> <c:set var= "Population" value= "-1"/> </c:otherwise> </c:choose> </jsp:root> ;
<?xml version= ' 1.0 '?>
<jsp:root
version= ' 2.0 ' xmlns:jsp= ' http://java.sun.com/JSP/Page ' Xmlns:util= "Urn:jsptagdir:/web-inf/tags"
xmlns:c= "Http://java.sun.com/jsp/jstl/core" >
<jsp: Directive.page contenttype= "text/html"/>
Appendix:
Commonly used directives in the label file:
Tag |
Similar to the JSP page directive, can be used to import common Java class libraries, etc. |
Include |
Import other label definition files |
Taglib |
Use other tags, such as jstl, spring tag, struts tag, etc. |
Attribute |
Define a property |
Variable |
Defines a variable that is visible in a JSP page, and the default range is nested, which indicates that the label is valid. Available options are at_begin and At_end |
Optional attributes for these directives
Body-content |
Label body handling mode, Optional: ' Empty ', ' tagdependent ' or ' scriptless ' |
Import |
Java class libraries used for import |
Pageencoding |
Set page encoding |
Iselignored |
Whether to ignore El expressions |
Dynamic-attributes |
A map for storing custom attributes, which is called a custom attribute: an implicitly-declared variable |
Language |
The scripting language used, must now be Java |
Display-name |
Label Signature |
Small-icon |
For tools |
Large-icon |
For tools |
Description |
Label function description |
Example |
Informal description of how the ' tag is used |
Optional properties for <jsp:directive:attribute>
Name |
Property name |
Required |
True or False |
Rtexprvalue |
True or false-Specifies whether Run-time expressions are supported |
Type |
Value type-default is Java.lang.String |
Fragment |
True or false-values are passed first to the container (false) and passed directly to the label processing method (True) |
Description |
Attribute description |
Optional properties for <jsp:directive:variable>
Name-given |
Variable name (variable name when label is used) |
Name-from-attribute |
Specifies the name of an attribute, whose value are the name of the variable that would be available the calling JSP page . Exactly one of Name-given or name-from-attribute must be supplied. |
Alias |
A locally scoped variable which'll store the variable ' s value. Used only with Name-from-attribute. |
Variable-class |
Variable class. The default is java.lang.String. |
Declare |
Indicates whether the variable is declared in the calling JSP page or tag file. The Default is true. Not entirely clear what this means! |
Scope |
Variable range, optional at_begin (valid after tag in JSP page), At_end (valid in JSP page after tag) and NESTED. NESTED (default, valid in label) |
Description |
Variable description |