The example in this article describes the custom label usage in JSP. Share to everyone for your reference. Specifically as follows:
Here a simple write a custom tag, the advantage of their own definition of the tag is in the JSP page can be used to define their own functions, completely separated from the Java code
1. tld files are as follows:
The first is to write x.tld files, and when the project starts with the server, it checks to see if there are any *tld files in the project.
A TLD file written by
<?xml version= "1.0" encoding= "UTF-8"?> <taglib xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http:/" /www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/ Xml/ns/javaee/web-jsptaglibrary_2_1.xsd "version=" 2.1 > <!--defined version--> <tlib-version>1.0</tlib-ver sion> <!--definition name--> <short-name>apsliyuan</short-name> <!--define URI--> <uri>http:/ /my.oschina.net/aps</uri> <!--define your own class--> <tag> <!--name is the label name that is displayed in the JSP, <APS:HELLOWTAG/&G T --> <name>hellowTag</name> <!--The full path of the label class you wrote--> <tag-class>cn.itcast.apsliyuan.tag . Hellowttag</tag-class> <!--jsp in the topic is not to display content, there are four properties, if empty. tags in the JSP page can not define their own content, otherwise it will be reported Servlet.service () for Servlet JSP threw exception org.apache.jasper.JasperException:/ind Ex.jsp (12,8) According to TLD, tag Aps:hellowtag must being empty, but is nOT exception--> <body-content>JSP</body-content> </tag> <!--there is no attribute written here, there is time to make up the--> <t Ag> <name>ApsliyuanTag</name> <tag-class>cn.itcast.apsliyuan.tag.apsliyuantag</tag-class > <body-content>JSP</body-content> </tag> </taglib>
2. Custom Label class Java code is as follows:
Custom Label class package Cn.itcast.apsliyuan.tag;
Import java.io.IOException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import Javax.servlet.jsp.tagext.TagSupport; public class Hellowttag extends tagsupport{/** * */private static final long Serialversionuid = 1781703371130
382609L; @Override public int doStartTag () throws Jspexception {//TODO auto-generated a stub jspwriter out = Pagec
Ontext.getout ();
SimpleDateFormat format=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
try {out.print (Format.format (New Date ());
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
return eval_body_include; @Override public int Doendtag () throws Jspexception {//TODO auto-generated a stub jspwriter out = Pag
Econtext.getout ();
try {out.print ("<br/>
3. JSP introduces itself to define the label code as follows:
JSP introduces its own defined label
<%@ page language= "java" contenttype= "text/html charset=utf-8" pageencoding= "UTF-8"
% >
<% @taglib uri= "Http://my.oschina.net/aps" prefix= "APS"%>
<% @taglib uri= "http://java.sun.com /jsp/jstl/core "prefix=" C "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
4. The TagSupport code is as follows:
Look at the source code in TagSupport, this is adapter mode public class TagSupport implements Iterationtag, Serializable {public static final Tag F
Indancestorwithclass (Tag from,//TCK signature test fails with generics @SuppressWarnings ("unchecked")
Class Klass) {Boolean isinterface = false;
if (from = = NULL | |
Klass = = NULL | | (! Tag.class.isAssignableFrom (Klass) &&! (Isinterface = Klass.isinterface ()))
{return null; for (;;)
{Tag tag = from.getparent ();
if (tag = = null) {return null;
if (isinterface && klass.isinstance (tag)) | |
Klass.isassignablefrom (Tag.getclass ()) return tag;
else from = tag; }/** * Default constructor, all subclasses are required to define only * a public constructor with the same s
Ignature, and to call the * superclass constructor.
* * This constructor was called by the code generated by the JSP * translator. * * Public TagSupport () {}/** * DefaUlt processing of the start tag, returning skip_body. * * @return Skip_body * @throws jspexception If a error occurs while processing this tag * * * @see Tag#dostart
Tag () */public int doStartTag () throws jspexception {return skip_body;
}/** * Default processing of the end tag returning eval_page. * * @return Eval_page * @throws jspexception If a error occurs while processing this tag * * * @see TAG#DOENDTA
G () */public int doendtag () throws jspexception {return eval_page;
}/** * Default processing for a body. * * @return Skip_body * @throws jspexception If a error occurs while processing this tag * * * @see Iterationta
G#doafterbody () */public int doafterbody () throws jspexception {return skip_body;
}//Actions related to body evaluation/** *.
* * @see tag#release () * */public void release () {parent = null;
id = null;
if (values!= null) {values.clear (); values = NULL;
}/** * Set The nesting tag of this tag.
* @param t the parent Tag.
* @see tag#setparent (TAG) */public void setparent (Tag t) {parent = t;
}/** * The tag instance most closely enclosing this tag instance.
* @see tag#getparent () * @return The parent tag instance or null */public tag getparent () {return parent;
}/** * Set The id attribute for this tag.
* * @param ID The String for the ID.
*/public void setId (String id) {this.id = ID; }/** * The value of the ID of this tag;
or null.
* * @return The value of the id attribute, or null/public String getId () {return id;
}/** * Set the page context.
* * @param pagecontext the PageContext.
* @see Tag#setpagecontext */public void Setpagecontext (PageContext pagecontext) {this.pagecontext = PageContext;
}/** * Associate a value with a String key.
* * @param k the key String. * @param o the value to associate. */public void SetValue (String k, Object o) {if (values = = null) {values = new hashtable<string, object> ()
;
} values.put (k, O);
}/** * Get A is the value associated with a key.
* * @param k the string key.
* @return The value associated with the key, or null.
*/Public Object GetValue (String k) {if (values = = null) {return null;
else {return values.get (k);
}/** * Remove A value associated with a key.
* * @param k the string key.
*/public void RemoveValue (String k) {if (values!= null) {values.remove (k);
}/** * Enumerate the keys for the values kept by this tag handler. * @return An enumeration of the keys for the values set, * or null or a empty enumeration if no values have
been set.
*/Public enumeration<string> getValues () {if (values = = null) {return null;
return Values.keys ();
}//Private fields Private Tag parent;
Private hashtable<string, object> values; /** * The value of the ID of this tag;
or null.
*/protected String ID;
Protected Fields/** * the PageContext.
* * protected PageContext PageContext;
}
return value of doStartTag
The value returned in doStartTag determines how the data in the body section is displayed.
Two return values:
0–skip_body– constants. does not show body.
1-evan_body_include containing the body part of the data, normal display.
3: There are also two return values in Doendtag
Decide if the following page section is displayed:
Skip_page: No longer displays the part of the page that follows.
Eval_page: Displays the PAGE section that follows.
I hope this article will help you with the JSP program design.