1. demonstrate traditional custom labels (earlier than jsp2.0)
Use custom tags to control whether the page content (TAG body) is output, and use the doStartTag () return value to control
Return this. SKIP_BODY; // ignore the label body.
Return this. EVAL_BODY_INCLUDE; // execute the label body
Control the output of the entire jsp using the doEndTag () Return Value Control
Return this. SKIP_PAGE; // The remaining jsp code after the page tag is skipped
Return this. EVAL_PAGE; // continue executing the remaining jsp code
Custom Tag implementation content (Tag body) loop output using the doAfterBody () defined in Tag sub-interface Iteration and the returned value EVAL_BODY_AGAIN, SKIP_BODY implementation
Overwrite the doStartTag () method and return EVAL_BODY_INCLUDE.
Override doAfterBody ()
[Html]
Public int doAfterBody () throws JspException {
Times ++;
Int result = this. EVAL_BODY_AGAIN;
If (times> 4 ){
Result = this. SKIP_BODY;
}
Return result;
}
Custom tag modification content (TAG body) EVAL_BODY_BUFFERED;
Tag processing class:
Inherit BodyTagSupport
Overwrite doStartTag () and return EVAL_BODY_BUFFERED;
Overwrite doEndTag ()
Public int doEndTag () throws JspException {
BodyContent bc = this. getBodyContent ();
String c = bc. getString ();
C = c. toUpperCase ();
JspWriter out = this. pageContext. getOut ();
Try {
Out. write (c );
} Catch (IOException e ){
Throw new RuntimeException (e );
}
Return this. EVAL_PAGE;
}
2. Requirement: implement a custom label function: Judge the date of a YYYY-MM-DD format modified to the following format output
Year: YYYY
Month: MM
Day: DD
Analysis:
Custom tag processing class (Demo5.java)
Package com. csdn. web. tag;
Import java. io. IOException;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. BodyContent;
Import javax. servlet. jsp. tagext. BodyTagSupport;
Import javax. servlet. jsp. tagext. Tag;
Public class Demo5 extends BodyTagSupport {
@ Override
Public int doEndTag () throws JspException {
BodyContent bc = this. getBodyContent ();
String B = bc. getString ();
String [] data = B. split ("-");
JspWriter out = this. pageContext. getOut ();
Try {
Out. println ("year:" + data [0] + "<br> ");
Out. println ("month:" + data [1] + "<br> ");
Out. println ("day:" + data [2] + "<br> ");
} Catch (IOException e ){
E. printStackTrace ();
}
Return Tag. EVAL_PAGE;
}
}
Display time to be converted 6.jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Taglib uri = "/csdn" prefix = "csdn" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> convert and output the date format </title>
</Head>
<Body>
<Csdn: demo5> 2012-11-17 </csdn: demo5>
</Body>
</Html>
Corresponding tag template csdn. tld
<? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
Version = "2.0">
<Tlib-version> 1.0 </tlib-version>
<Short-name> csdn </short-name>
<Uri>/csdn </uri>
<Tag>
<Name> demo5 </name>
<Tag-class> com. csdn. web. tag. Demo5 </tag-class>
<Body-content> JSP </body-content>
</Tag>
</Taglib>