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

📄 j-customtags-3-7.html

📁 这是用java编写自定义标签的一些源码和笔记
💻 HTML
📖 第 1 页 / 共 2 页
字号:
</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="3.简单标签" src="imagemaster/titlebar3.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15">&nbsp;</td><td bgcolor="ffffff" width="12">&nbsp;</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>第 5 步:实现 doStartTag() 方法</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr>   第 7 页(共9 页)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>
          

  标签开始时调用 <code>doStartTag()</code> 方法 —— 从开发人员的角度看,这是当引擎遇到 

 <code>&lt;map:mapDefine&gt;</code> 时发生的。如果 <code>doStartTag()</code> 返回 <code>SKIP_BODY</code>,那么将不处理标签正文。 

 如果它返回一个 <code>EVAL_BODY_INCLUDE</code>,那么将处理正文。
</p>
<p>
						
<code>MapDefine</code> 类的 <code>doStartTag()</code> 方法完成以下工作:
</p>
<ul>
<li>根据 <code>type</code> 属性确定要创建的 map 的属性。</li>
<li>根据 <code>scope</code> 属性确定新的 map 对象放在什么范围内。</li>
<li>根据 <code>id</code> 属性确定新 map 对象要放入的范围的名字。</li>
</ul>
<p>
 让我们更详细地分析这个过程。<code>MapDefine</code> 类检查 <code>type</code> 属性是设置为 <code>FASTTREE</code>、<code>HASH</code>、<code>TREE</code> 还是 <code>FASTHASH</code>。然后创建相应的 map,如下所示:

 

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

     /* String constants for the different types of maps we support */

     public static final String FASTHASH = "FASTHASH";

     public static final String FASTTREE = "FASTTREE";

     public static final String HASH = "HASH";

     public static final String TREE = "TREE";

     

     /** The map we are going to create */

     private Map map = null;

     

     /** The member variable that holds the type attribute */

     private String type = FASTTREE;

     ...

 

     public int doStartTag() throws JspException {

 

         /** Based on the type attribute, determines which type of Map to create */

         if (type.equalsIgnoreCase(FASTTREE)) {

             map = new FastTreeMap();

         } else if (type.equalsIgnoreCase(HASH)) {

             map = new HashMap();

         } else if (type.equalsIgnoreCase(TREE)) {

             map = new TreeMap();

         } else if (type.equalsIgnoreCase(FASTHASH)) {

             map = new FastHashMap();

         }

         

 </code>
</pre>
<p>
  

 

 

 然后,用 <code>id</code> 和 <code>scope</code> 属性将 hashmap 以一个给定的名字设置到一个给定范围中:

 

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

 

     private String id;

     private String scope;

 

     public int doStartTag() throws JspException {

 

         ...        

         if (scope == null){

             pageContext.setAttribute(id, map);

         }else if("page".equalsIgnoreCase(scope)){

             pageContext.setAttribute(id, map);

         }else if("request".equalsIgnoreCase(scope)){

             pageContext.getRequest().setAttribute(id, map);

         }else if("session".equalsIgnoreCase(scope)){

             pageContext.getSession().setAttribute(id, map);

         }else if("application".equalsIgnoreCase(scope)){

             pageContext.getServletContext().setAttribute(id, map);

 

         }

         

         return EVAL_BODY_INCLUDE;

     }

 

 </code>
</pre>
<p>
  

 

 如果范围属性是 <code>null</code>,那么 map 将放入页范围。否则,参数将放入通过 <code>scope</code> 属性传递的范围名所对应的范围中。 

 </p>
<p>

 到目前为止,我们已经有一个非常简单的标签,它有三个属性:<code>id</code>、<code>scope</code> 和 <code>type</code>。

我们将用给定的名字将 map 放到一个范围中。但是,我们还有一件事没做,就是声明 scriptlet 变量。

 为了做到这一点,需要向 TLD 再添加一项,这就是我们在下一小节中所要做的事。 

 

 


          </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="index3.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-3-6.html"><img alt="上页" border="0" src="../i/previous.gif" name="bottomprevious"></a></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-3-8.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 + -