`
igogogo9
  • 浏览: 247957 次
  • 性别: Icon_minigender_1
  • 来自: 海口市
社区版块
存档分类
最新评论

jsp自定义标签(四)

阅读更多
简单标签的生存周期概述

如果像我第一次使用标签时那样,您可能发现不容易将转换引擎生存周期与运行时实际发生的事情对应起来。 像我一样,您可能看到很多试图说明这些概念的图表,这些图表看起来可能像下面这样。

这里是已最小化的图像

这个图表对我没什么意义,但是生成的代码有意义,所以我们将在这一小节中分析这些代码。(现在我觉得这些图表有意义了,但是我认为这些代码更能说明问题,在我们完成这一小节时,您可能想再次分析这个图表。)

您可能还记得,JSP 页实际上是伪装的 servlet。JSP 文件在使用之前转换为 servlet。 下面是一个名为 testMapDefine.jsp 的小型 JSP 页,我们将用它来展示并测试在前面一节中开发的 MapDefineTag 标签:

 <%@taglib uri="map" prefix="map"%>   <map:mapdefine id="employee"> 
The employee is <%=employee%>
<% 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%>
</map:mapdefine>

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

 <web-app> ... <taglib> <taglib-uri>map</taglib-uri> <taglib-location>/WEB-INF/tlds/map.tld</taglib-location> </taglib> ... </web-app> 

有另一种导入标签的方法。我认为这个方法更有用,因为它允许为 URI 指定一个短名,而短名容易记忆。 注意 taglib 是在其 TLD 文件上下文中描述的。在 WEB-INF 目录中的 map.tld 文件看起来像下面这样:

   <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> ... </tag></taglib> 

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

 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("
\n The employee is "+ (employee) +"
\n"); employee.put("firstName", "Kiley"); employee.put("lastName", "Hightower"); employee.put("age", new Integer(33)); employee.put("salary", new Float(22.22)); out.print("
\n The employee is "+ (employee) +"
\n"); } employee = (java.util.Map)pageContext.findAttribute("employee"); ... } catch (java.lang.Throwable _jsp_e) { pageContext.handlePageException(_jsp_e); ... } ... }

生成的 JSP servlet 声明了名为 tag0、类型为 trivera.tags.map.MapDefineTag 的一个本地变量。 然后它创建标签的一个实例、设置页上下文、设置父标签为 null,并设置 ID 为 employee。然后,生成的 servlet 调用 doStartTag() 方法,它检查返回类型是否设置为 Tag.SKIP_BODY。如果是,那么容器就不对标签的正文(在这里就是 if 块)进行判断。如果它返回 EVAL_BODY_INCLUDE,就像我们的标签那样,容器就将处理正文。 可以用这种技术有条件地加入标签的正文 —— 即控制流程。

注意生成的 servlet 有一个名为 employee 的本地变量, 根据标签的 id 属性将它设置为 employee。因此,转换引擎在 转换 时而不是运行时定义了一个名为 employee 的本地变量。这个概念使很多新人感到迷惑。

理解嵌套标签

前面的 JSP 页示例使用 JSP scriptlet 向 employee map 添加项。如果可以用另一个标签来完成就好了。 让我们定义一个名为 MapEntryTag 的嵌套标签,它通过调用 getParent() 并将它转换为 MapDefineTag 而得到其父标签。

MapDefineTag 定义了 getMap() 方法,它返回新创建的 map。 doEndTag() 中的嵌套 MapEntryTag 使用 MapDefineTaggetMap() 方法向 map 中增加值,如下所示:

 public class MapEntryTag extends TagSupport { String type = "java.lang.String"; String id; String value; String name; String property; String scope; public int doEndTag() throws JspException { /* Grab the MapDefineTag using the getParent method and cast it to a MapDefineTag.*/ MapDefineTag mapDef = (MapDefineTag) this.getParent(); Object objectValue = null; ... /* Instantiate a new String, Integer or Float based on the type. */ if (type.equals("java.lang.String")) { objectValue = value; } else if (type.equals("java.lang.Integer")) { Integer intValue = Integer.valueOf(value); objectValue = intValue; } else if (type.equals("java.lang.Float")) { Float floatValue = Float.valueOf(value); objectValue = floatValue; } /* Put the new entry into the map. */ mapDef.getMap().put(id,objectValue); return EVAL_PAGE;  

在上述代码的基础上,并假定在 TLD 文件中加入了必要的项,则可以像这样使用我们的标签:

 <%@taglib uri="map" prefix="map"%>   <map:mapdefine id="employee"> <map:mapentry value="Jennifer" id="firstName"> <map:mapentry value="Wirth" id="lastName"> <map:mapentry type="java.lang.Integer" value="33" id="age"> <map:mapentry type="java.lang.Float" value="22.22" id="salary"> </map:mapentry> The employee is set as <%=employee%> 
</map:mapentry></map:mapentry></map:mapentry></map:mapdefine>

上述清单定义一个 employee map,其中带有针对 firstNamelastNameagesalary 各三项,它们的类型分别为 StringStringIntegerFloat

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics