能处理标记内容的标记:
1 <abc:tagWithNoBody attribute="value"/>
2 <abc:tagWithBody attribute="value">
3 This is some body content that the tag handler can operate upon.
4 </abc:tagWithBody>
在 DateTag 例子中不需要加入正文内容,因为它只显示当前日期。因此,我们的处
理程序类将实现 Tag 接口(
一般是通过扩展 TagSupport 类)。如果我们要创建一个可以
处 理 正 文 的 标 记 , 那 么 我 们 就 需 要 实 现 BodyTag 接 口 ( 一 般 是 通 过 扩 展
BodyTagSupport 类)
。清单 1
显示了 DateTag 的处理程序类:
清单 1. 标记处理程序类
1 package myTags;
2 import javax.servlet.jsp.tagext.*;
3 import javax.servlet.jsp.*;
4 import javax.servlet.http.*;
5 import java.text.*;
6 import java.util.*;
7 public DateTag extends TagSupport {
8
9 public int doStartTag() throws javax.servlet.jsp.JspException {
10 HttpServletRequest req;
11 Locale locale;
12 HttpJspPage g;
13 DateFormat df;
14 String date;
15 JSPWriter out;
16
17 req = ( HttpServletRequest )pageContext.getRequest();
18 locale = req.getLocale();
19 df = SimpleDateFormat.getDateInstance(
20 SimpleDateFormat.FULL,locale );
21 date = df.format( new java.util.Date() );
22
23 try {
24 out = pageContext.getOut();
25 out.print( date );
26 } catch( IOException ioe ) {
27 throw new JspException( "I/O Error : " + ioe.getMessage() );
28 }//end try/catch
29 return Tag.SKIP_BODY;
30 }//end doStartTag()
31 }//end DateTag
32