📄 j-customtags-5-1.html
字号:
<TD bgcolor="#ffffff" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#cc6633" height="3" width="150"><IMG src="../i/c.gif" width="150" height="3"></TD>
</TR>
<TR>
<TD bgcolor="#333333" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#000000" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG src="../i/c.gif" width="150" height="2"></TD>
</TR>
</TABLE>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><font size="-1" face="Verdana, sans-serif"><a href="http://www.ibm.com/">IBM</a> : <a href="/developerWorks/cn/index.shtml">developerWorks 中国网站</a> : <a href="/developerWorks/cn/java/index.shtml">Java</a> : <a href="/developerWorks/cn/cnedu.nsf/java-onlinecourse-bytitle?OpenView&Count=50">教学 - 在线教程</a></font>
<br>
<img alt="掌握自定义 JSP 标签" src="imagemaster/masthead.jpg"></td><td width="*" valign="bottom" align="right"><a border="0" href="j-customtags.zip"><img alt="下载 ZIP 文件" border="0" src="../i/icon-zip.gif"></a><a target="_blank" href="../tutorial_eng/index.html" border="0"><img alt="英文原文" border="0" src="../i/icon-source.gif"></a>
</td>
</tr>
<tr>
<td height="2" colspan="2"><img height="2" width="1" src="../i/c.gif"></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index5.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-customtags-7-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xprevious.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-customtags-5-2.html"><img alt="下页" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="5.正文标签" src="imagemaster/titlebar5.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15"> </td><td bgcolor="ffffff" width="12"> </td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>介绍</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr> 第 1 页(共3 页)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>
可以编写标签处理程序对其 <i>正文内容</i> 进行操作。记住,标签的正文内容是 JSP 页中出现在自定义标签的开始和结束标签之间的数据。操纵其正文的标签称为 <i>正文标签</i>。编写正文标签处理程序比简单标签处理程序要复杂。
</p>
<p>
<b>注意:</b> 记住,简单标签也可以有正文。惟一的不同是简单标签不能读取或者操纵其正文。
</p>
<p>
要编写正文标签处理程序,必须实现 <code>BodyTag</code> 接口。<code>BodyTag</code> 实现了 <code>Tag</code> 实现的所有方法 (详见 <a href="j-customtags-3-3.html">第 1 步:创建一个实现了 Tag 接口的标签处理程序</a> ),而且还实现了另外两个处理正文内容的方法:
</p>
<table border="1">
<tr>
<th width="225pt">方法</th><th width="225pt">目的</th>
</tr>
<tr>
<td width="225pt">
<code>void setBodyContent(BodyContent b)</code>
</td><td width="225pt">
<code>bodyContent</code> 属性的 Setter 方法。
</td>
</tr>
<tr>
<td width="225pt">
<code>void doInitBody()</code>
</td><td width="225pt">
准备对正文进行判断。每次调用标签时,在获得新的 <code>BodyContent</code> 并通过 <code>setBodyContent()</code> 对其进行设置之后调用一次。
如果没有请求正文内容就不调用,因为 <code>doStartTag()</code> 返回 <code>EVAL_BODY_BUFFERED</code>。
</td>
</tr>
</table>
<p>
就像 <code>Tag</code> 接口有 TagSupport 类一样,<code>BodyTag</code> 接口有
<code>BodyTagSupport</code> 类。
因此,正文标签处理程序只需要覆盖它们要使用的方法。<code>BodyTagSupport</code>
类继承了 TagSupport 并实现了 <code>BodyTag</code> 接口。这使得编写正文标签处理程序更容易了。<code>BodyTagSupport</code> 定义了 <code>get</code>/<code>setBodyContent()</code> 和一个 protected <code>bodyContent</code> 实例变量。
</p>
<p>
<code>BodyTagSupport</code> 类重新定义了 <code>doStartTag()</code> 生存周期方法以返回 <code>EVAL_BODY_BUFFERED</code>。
通过返回 <code>EVAL_BODY_BUFFERED</code>,<code>doStartTag()</code> 请求创建一个新的缓冲区 —— 即一个 <code>BodyContent</code>。
</p>
<p>
<code>BodyContent</code> 是一个包含运行时正文判断结果的缓冲区。
<code>BodyContent</code> 继承了 <code>JspWriter</code> 并作为标签正文的隐式 <code>out</code>。因此,JSP 容器创建 <code>BodyContent</code> 的一个实例,并且在处理标签的正文内容时,它写入这个实例而不是根 <code>JspWriter</code> 中。
因此,在标签中使用隐式对象 <code>out</code> 时,实际上使用的是 <code>BodyContent</code> 对象而非 <code>JspWriter</code> (<code>JspWriter</code> 是页的隐式 <code>out</code> )。
</p>
<p>
可以从 <code>BodyContent</code> 得到判断后的正文,它是一个 <code>String</code>。<code>BodyContent</code> 是在运行时由容器调用页的 <code>pageContext</code> 的 <code>pushBody()</code> 和 <code>popBody()</code> 方法创建的 (只有在 <code>doStartTag()</code> 返回 <code>EVAL_BODY_BUFFERED</code> 时才调用<code>pushBody()</code>)。
因此,<code>BodyContent</code> 是在 <code>JspWriter</code> 和 <code>BodyContent</code> 的一个嵌套结构中的。
(外面的 <code>out</code> 可以是另一个 <code>BodyContent</code> 对象,因为 <code>BodyContent</code> 是一个 <code>JspWriter</code>。) 通过 <code>setBodyContent()</code> 方法将 <code>BodyContent</code> 提供给正文标签处理程序。
向正文标签处理程序传递一个 <code>BodyContent</code> 实例 (通过 <code>setBodyContent()</code>) 并可以决定如何处理它。
可以对它做进行一步处理、放弃它、将它发送给浏览器等。
</p>
<p>
已经介绍了足够的背景知识了,该分析代码了!在下面几小节中我们将分析一个简单正文标签的例子。
</p>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index5.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-customtags-7-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xprevious.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-customtags-5-2.html"><img alt="下页" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">关于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律条款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">联系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
<script src="//www.ibm.com/common/stats/stats.js" language="JavaScript1.2" type="text/javascript"></script>
<noscript>
<img border="0" alt="" height="1" width="1" src="//stats.www.ibm.com/rc/images/uc.GIF?R=noscript"></noscript>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -