· [Java论坛][安全论坛][数据库论坛][操作系统论坛]
· [专题] IBM Rational高峰论坛 Windows 7十大功能
· [专题] 史上就危险7月 微软 BizSpark 计划介绍
· [IT技术周刊][IT资源下载专区][病毒求助专区]
· [热点] 我也能做CTO_赛迪连载 赛迪七夕特别行动
· [热点] Chrome 4.0采用V8引擎 Java开发编程规范

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

发布时间:2004.11.25 10:53     来源:飞思科技    作者:孙卫琴

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

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

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

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

    以下是在tilestaglibs应用中使用标签的步骤。

    (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-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

    (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

<%@ 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>

    例程16-11 product.jsp

<%@ 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>

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

本文选自飞思图书《精通Struts:基于MVC的Java Web设计与开发》


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
资讯 通信 IT产品 IT技术 信息化
专题:扭亏为盈 联想09年Q2翻番
·专题:Tech·Ed 2009微..
·直播:2009互联网大会..
·迅雷搜狐"互搏" 谁动了..
专题:诺基亚危局已现 或重蹈摩托覆辙
·六股势力角逐4G标准 中..
·专题:联通iPhone并不贵..
·专题:排排坐开商店 三..
商务演示需求分析 多媒体会议室必备投影
·VMware在京隆重举行2009..
·专题:笔记本频道10月热..
·专题:学生机市场 惠普 ..
专题:Tech.Ed 2009微软技术大会
·专题:2009 SYBASE 亚太..
·专题:微软新一代桌面操..
·专题:2009年第3届CSDN..