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

📄 j-customtags-3-9.html

📁 这是用java编写自定义标签的一些源码和笔记
💻 HTML
📖 第 1 页 / 共 2 页
字号:

 <%@taglib uri="map" prefix="map"%>

 <html>

 <head><title>Test Map Define</title></head>

 <body>

 

 <map:mapDefine id="employee">

     <br />

     The employee is <%=employee%> <br />

     <%

     employee.put("firstName", "Kiley");

     employee.put("lastName", "Hightower");

     employee.put("age", new Integer(33));

     employee.put("salary", new Float(22.22));

     %>

     The employee is <%=employee%> <br />

 </map:mapDefine>

        

 </body>

 </html>

 </code>
</pre>
<p>
  

 

 注意这一页用 <code>map</code> 的 URI 导入了一个自定义标签。之所以能这样是因为我们在 web.xml 文件中声明了这个 TLD,如下所示:



 

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

 &lt;web-app&gt;

         ...

     &lt;taglib&gt;

         &lt;taglib-uri&gt;map&lt;/taglib-uri&gt;

         &lt;taglib-location&gt;/WEB-INF/tlds/map.tld&lt;/taglib-location&gt;

     &lt;/taglib&gt;

 ...

 </code>
</pre>
<p>
  

 

 有另一种导入标签的方法。我认为这个方法更有用,因为它允许为 URI 指定一个短名,而短名容易记忆。

 注意 taglib 是在其 TLD 文件上下文中描述的。在 <code>WEB-INF</code> 目录中的 <code>map.tld</code> 文件看起来像下面这样:

 

 

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

 &lt;?xml version="1.0" encoding="UTF-8"?&gt;

 &lt;!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
 "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"&gt;

 

 &lt;taglib&gt;

 

    &lt;tlib-version&gt;1.0&lt;/tlib-version&gt;

    &lt;jsp-version&gt;1.2&lt;/jsp-version&gt;

    &lt;short-name&gt;map&lt;/short-name&gt;

 

    &lt;tag&gt;

       &lt;name&gt;mapDefine&lt;/name&gt;

       &lt;tag-class&gt;trivera.tags.map.MapDefineTag&lt;/tag-class&gt;

       &lt;body-content&gt;JSP&lt;/body-content&gt;

    ...

 </code>
</pre>
<p>

 当 JSP 转换器遇到自定义标签 <code>mapDefine</code> 时,它将根据 <code>taglib</code> 指令和在 web.xml 文件中指定的 TLD 文件查询 TLD 文件。然后在生成的 servlet 中加入下面的代码:

 

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

 public class _testmapdefine__jsp extends ...JavaPage{

 

   public void _jspService(HttpServletRequest request, 
   	HttpServletResponse response) throws...{

 

     trivera.tags.map.MapDefineTag tag0 = null;

     java.util.Map employee = null;

     try {

       ...

       if (tag0 == null) {

         tag0 = new trivera.tags.map.MapDefineTag();

         tag0.setPageContext(pageContext);

         tag0.setParent((javax.servlet.jsp.tagext.Tag) null);

         tag0.setId("employee");

       }

 

       int includeBody = tag0.doStartTag();

       if (includeBody != javax.servlet.jsp.tagext.Tag.SKIP_BODY) {

         employee = (java.util.Map)pageContext.findAttribute("employee");

 

         out.print("&lt;br /&gt; \n The employee is "+ (employee) +"&lt;br /&gt; \n");        

 

     employee.put("firstName", "Kiley");

     employee.put("lastName", "Hightower");

     employee.put("age", new Integer(33));

     employee.put("salary", new Float(22.22));

     

         out.print("&lt;br /&gt; \n The employee is "+ (employee) +"&lt;br /&gt; \n");        

       }

       employee = (java.util.Map)pageContext.findAttribute("employee");

       ...

     } catch (java.lang.Throwable _jsp_e) {

       pageContext.handlePageException(_jsp_e);

     ...

   }

 ...

 }

 

 </code>
</pre>
<p>
  

 

 生成的 JSP servlet 声明了名为 <code>tag0</code>、类型为 <code>trivera.tags.map.MapDefineTag</code> 的一个本地变量。

 然后它创建标签的一个实例、设置页上下文、设置父标签为 null,并设置 ID 为 <code>employee</code>。然后,生成的 servlet 调用 <code>doStartTag()</code> 方法,它检查返回类型是否设置为 <code>Tag.SKIP_BODY</code>。如果是,那么容器就不对标签的正文(在这里就是 <code>if</code> 块)进行判断。如果它返回 <code>EVAL_BODY_INCLUDE</code>,就像我们的标签那样,容器就将处理正文。 

 可以用这种技术有条件地加入标签的正文 —— 即控制流程。

 

 </p>
<p>

 注意生成的 servlet 有一个名为 <code>employee</code> 的本地变量, 根据标签的 <code>id</code> 属性将它设置为 <code>employee</code>。因此,转换引擎在 <i>转换</i> 时而不是运行时定义了一个名为 <code>employee</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-4-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="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-8.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 + -