采用Tiles:Insert标签创建复合式网页

Tiles标签库的tiles:insert标签和JSP include指令具有相同的功能,也能把其他的JSP页面插入到当前页面中。例如,以下两条语句的作用是相同的:

<ccid_nobr>

<ccid_code><jsp:include page="indexContent.jsp"/>
<tiles:insert page="indexContent.jsp" flush="true"/></ccid_code>
</ccid_nobr>

    <tiles:insert>标签的page属性指定被插入的JSP文件,flush属性的可选值包括true和false,当flush属性为true,表示在执行插入操作之前,先调用当前页面的输出流的flush()方法。 </tiles:insert>

    提示:本节介绍的 tilestaglibs应用的源程序位于配套光盘的sourcecode/tilestaglibs/version3/tilestaglibs目录 下。如果要在Tomcat上发布这个应用,只要把version3目录下的整个tilestaglibs子目录拷贝到<catalina_home>/webapps目录下即可。 </catalina_home>

    以下是在tilestaglibs应用中使用<tiles:insert>标签的步骤。 </tiles:insert>

    (1)安装Tiles标签库所需的文件
    在Struts的下载软件中包含了运行Tiles标签库所需的文件。如果Web应用中使用了Tiles标签库,以下文件必须位于WEB-INF/lib目录中:
    ·struts.jar
    ·commons-digester.jar
    ·commons-beanutils.jar
    ·commons-collections.jar
    ·commons-logging.jar

    此外,应该把Tiles标签库的定义文件struts-tiles.tld拷贝到WEB-INF目录下。

    (2)在web.xml文件中配置如下<taglib>元素: </taglib>

<ccid_nobr>

<ccid_code><taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib></ccid_code>
</ccid_nobr>

    (3)创建index.jsp和product.jsp文件
    修 改16.2节的例程16-8(index.jsp)和例程16-9(product.jsp),在index.jsp和product.jsp文件的开 头,通过%@ taglib指令引入Tiles标签库,然后把源代码中的JSP include指令改为tiles:insert标签。例程16-10和例程16-11分别为修改后的index.jsp和product.jsp文件。

    例程16-10 ndex.jsp

<ccid_nobr>

<ccid_code><%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<html>
<head>
<title>TilesTaglibs Sample</title>
</head>
<body >
<%-- One table lays out all of the content for this page --%>
<table width="100%" height="100%">
<tr>
<%-- Sidebar section --%>
<td width="150" valign="top" align="left" bgcolor="#CCFFCC">
<tiles:insert page="sidebar.jsp" flush="true"/>
</td>
<%-- Main content section --%>
<td height="100%" width="*">
<table width="100%" height="100%">
<tr>
<%-- Header section --%>
<td valign="top" height="15%">
<tiles:insert page="header.jsp" flush="true"/>
</td>
<tr>
<tr>
<%-- Content section --%>
<td valign="top" height="*">
<tiles:insert page="indexContent.jsp" flush="true"/>
</td>
</tr>
<tr>
<%-- Footer section --%>
<td valign="bottom" height="15%">
<tiles:insert page="footer.jsp" flush="true"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html></ccid_code>
</ccid_nobr>

    例程16-11 product.jsp

<ccid_nobr>

<ccid_code><%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<html>
<head>
<title>TilesTaglibs Sample</title>
</head>
<body >
<%-- One table lays out all of the content for this page --%>
<table width="100%" height="100%">
<tr>
<%-- Sidebar section --%>
<td width="150" valign="top" align="left" bgcolor="#CCFFCC">
<tiles:insert page="sidebar.jsp" flush="true"/>
</td>
<%-- Main content section --%>
<td height="100%" width="*">
<table width="100%" height="100%">
<tr>
<%-- Header section --%>
<td valign="top" height="15%">
<tiles:insert page="header.jsp" flush="true"/>
</td>
<tr>
<tr>
<%-- Content section --%>
<td valign="top" height="*">
<tiles:insert page="productContent.jsp" flush="true"/>
</td>
</tr>
<tr>
<%-- Footer section --%>
<td valign="bottom" height="15%">
<tiles:insert page="footer.jsp" flush="true"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html></ccid_code>
</ccid_nobr>

    从例程16-10和例程16-11可以看出,用tiles:insert标 签取代JSP include指令来创建复合式页面,代码仅有稍微的差别,两者的利弊也很相似。单纯使用tiles:insert标签来创建复合式页面,还没有充分发挥 Tiles框架的优势。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值