⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j-customtags-6-4.html

📁 这是用java编写自定义标签的一些源码和笔记
💻 HTML
📖 第 1 页 / 共 2 页
字号:
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>
          

 
<code>doAfterBody()</code> 通过在没有可迭代的项时返回 <code>SKIP_BODY</code> 实现了迭代。如果还有项,那么它就返回 <code>EVAL_BODY_AGAIN</code>。只要 <code>doAfterBody()</code> 返回 <code>EVAL_BODY_AGAIN</code>,容器就会继续对正文进行判断,如下所示:

 

 </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  

 public int doAfterBody() throws JspException {

 

     /** Process body */

     ...

 

     /** Write the processed  buffer to the previous out */

     ...    

 

     if (iter.hasNext() == false) {

         return SKIP_BODY;

     } else {

 

         map = (Map) iter.next();

         return EVAL_BODY_AGAIN;

     }

 }

 

 </code>
</pre>
<p>
  

 

 
<code>doAfterBody()</code> 方法用 <code>getBodyContent()</code> 抓取正文,然后用正文内容的 <code>getString()</code> 方法得到字符串形式的内容。

 下一步它清除正文内容缓冲区,用 <code>StringTokenizer</code> 查找以 <code>${</code> 开始的字符串。

 它还创建一个名为 <code>buffer</code> 的 <code>StringBuffer</code>。 


 当它迭代 tokenizer 中的字符串时,将它们附加到缓冲区中。</p>
<p>

 如果 tokenizer 中的字符串以 <code>${</code>, <code>doAfterBody()</code> 开始,那么它就从字符串中提取键,并用这个键在 map 中查找值。它调用值对象的 <code>toString()</code> 方法将 map 中的值转换为字符串,并将结果附加到缓冲区中。下面的清单展示了这个过程是如何进行的。

 </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  

 /** Process body */

 

 /* Get and clear the body */

 BodyContent body = this.getBodyContent();

 String content = body.getString();

 body.clearBody();

 

 /* Process the body with a String tokenizer */

 StringTokenizer token = new StringTokenizer(content);

 /* Create an output buffer of the processed body */

 StringBuffer buffer = new StringBuffer(content.length() * 2);

 

 /* Iterate over the strings from the tokenizer 

    and put them into the output buffer. */

 while (token.hasMoreTokens()) {

     String tok = token.nextToken();

     

     /* See if the String contains the special substring "${" */

     if (tok.startsWith("${")) {

     

         /* Parse the key out of the string */

         String key = tok.substring(2, tok.length() - 1);

 

         /* Use the key to look up the object in the map */

         Object value = (String) map.get(key);

         String svalue = tok;

         

         /* If the value is not null, 

            get the value's string representation */

         if (value != null) {

             svalue = value.toString();

         }

         

         /* Add the string representation of the value 

            to the output buffer */

         buffer.append(svalue + " ");

     } else {

         buffer.append(tok + " ");

     }

 }

 

 </code>
</pre>
<p>
  

 

 
<code>doAfterBody()</code> 方法在构建了输出缓冲区后,就将它输出到前面的 <code>out</code> 中,如下所示:

 

 </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  

     try {

         this.getPreviousOut().print(buffer.toString());

     } catch (IOException e) {

         throw new JspException(e);

     }

 

 </code>
</pre>
<p>
  

 

 这样,每一次迭代都处理正文并将它输出到前面的 <code>out</code> 中。如果前面的 <code>out</code> 是根 <code>JspWriter</code>,那么它就会写到浏览器中。

 


          </p>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD align="right" colspan="6"><a border="0" onMouseOver="iOver('topnextsection'); iOver('bottomnextsection'); self.status=nextsectionblurb; return true;" onMouseOut="iOut('topnextsection'); iOut('bottomnextsection'); self.status=''; return true;" href="j-customtags-7-1.html"><img alt="下一章" src="../i/nextsection.gif" border="0" name="bottomnextsection"></a></TD>
</TR>
<TR>
<TD width="100%" colspan="5"></TD><TD width="108" height="1" bgcolor="#000000" align="right"><IMG alt="" height="1" width="108" 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="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="index6.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"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-customtags-6-3.html"><img alt="上页" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xnext.gif"></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 + -