采用JSP的Include指令创建复合式网页

    为了减少代码的冗余, 可以把index.jsp和product.jsp中相同部分放在单独的JSP文件中,然后在index.jsp和product.jsp文件中通过 JSP include指令把其他JSP文件包含进来。图16-4和图16-5分别显示了index.jsp和product.jsp文件包含的其他JSP文件。

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


图16-4 index.jsp包含的其他JSP文件


图16-5 product.jsp包含的其他JSP文件

    由图16-4和图16-5可以看出,在index.jsp和product.jsp 中均包含header.jsp、sidebar.jsp和footer.jsp,仅仅网页主体部分包含的JSP文件不同。例程16-3、16-4、 16-5、16-6、16-7、16-8和16-9分别为header.jsp、footer.jsp、sidebar.jsp、 indexContent.jsp、productContent.jsp、index.jsp和product.jsp的源代码。

    例程16-3 header.jsp

      
      
       
       <font size="6">Welcome to ABC Inc.</font>
       
       
<hr>

    例程16-4 footer.jsp

      
      
       
       <hr>
       
       
Thanks for stopping by!

    例程16-5 sidebar.jsp

      
      
       
       <%@ page contentType="text/html; charset=UTF-8" %>
       
       
<table >
<tr>
<%-- Sidebar top component --%>
<td width="150" height="65" valign="top" align="left">
<a href=""><img src="chinese.gif" border="0"/></a>
<a href=""><img src="usa.gif" border="0"/></a>
</td>
</tr>
<tr>
<%-- Sidebar bottom component --%>
<td>
<table>
<tr>
<td>
<font size="5">Links</font><p>
<a href="index.jsp">Home</a><br>
<a href="product.jsp">Products</a><br>
<a href="">Hot Link1</a><br>
<a href="">Hot Link2</a><br>
<a href="">Hot Link3</a><br>
</td>
</tr>
</table>
</td>
</tr>
</table>

    例程16-6 indexContent.jsp

      
      
       
       <font size="4">Page-specific content goes here</font>
      
      

    例程16-7 productContent.jsp

      
      
       
       <font size="4">Products</font> <p>
       
       
<li>product1</li> <br>
<li>product2</li> <br>
<li>product3</li> <br>

    例程16-8 index.jsp

      
      
       
       <%@ page contentType="text/html; charset=UTF-8" %>
       
       
<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">
<jsp:include page="sidebar.jsp"/>
</td>
<%-- Main content section --%>
<td height="100%" width="*">
<table width="100%" height="100%">
<tr>
<%-- Header section --%>
<td valign="top" height="15%">
<jsp:include page="header.jsp"/>
</td>
<tr>
<tr>
<%-- Content section --%>
<td valign="top" height="*">
<jsp:include page="indexContent.jsp"/>
</td>
</tr>
<tr>
<%-- Footer section --%>
<td valign="bottom" height="15%">
<jsp:include page="footer.jsp"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

    例程16-9 product.jsp

      
      
       
       <%@ page contentType="text/html; charset=UTF-8" %>
       
       
<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">
<jsp:include page="sidebar.jsp"/>
</td>
<%-- Main content section --%>
<td height="100%" width="*">
<table width="100%" height="100%">
<tr>
<%-- Header section --%>
<td valign="top" height="15%">
<jsp:include page="header.jsp"/>
</td>
<tr>
<tr>
<%-- Content section --%>
<td valign="top" height="*">
<jsp:include page="productContent.jsp"/>
</td>
</tr>
<tr>
<%-- Footer section --%>
<td valign="bottom" height="15%">
<jsp:include page="footer.jsp"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

    采用JSP include指令来创建复合式页面,已经在提高代码可重用性方面迈出了正确的一步。index.jsp和product.jsp中包含的相同内容,被放 在单独的JSP页面中。index.jsp和product.jsp只需通过JSP include指令把这些相同内容包含进来,这样提高了代码的可重用性。但是JSP include指令不能完全避免代码冗余,例如从例程16-8 和例程16-9可以看出,index.jsp和product.jsp中仍然存在许多重复代码,仅仅粗体字标识的代码块不是重复代码。

    此外,和16.1节介绍的方案相比,尽管第二种方案减少了重复代码,但JSP文件的数量增加了,由原来的2个文件增加到7个文件,所以软件的复杂度也增加了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值