📄 j-customtags-3-9.html
字号:
<%@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">
<web-app>
...
<taglib>
<taglib-uri>map</taglib-uri>
<taglib-location>/WEB-INF/tlds/map.tld</taglib-location>
</taglib>
...
</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">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>map</short-name>
<tag>
<name>mapDefine</name>
<tag-class>trivera.tags.map.MapDefineTag</tag-class>
<body-content>JSP</body-content>
...
</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("<br /> \n The employee is "+ (employee) +"<br /> \n");
employee.put("firstName", "Kiley");
employee.put("lastName", "Hightower");
employee.put("age", new Integer(33));
employee.put("salary", new Float(22.22));
out.print("<br /> \n The employee is "+ (employee) +"<br /> \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 + -