创建动态Web页面的最基本的办法是为每个页面创建独立的JSP文件。图16-2和图16-3分别为tilestaglibs应用的主页index.jsp和产品页面product.jsp。
本节介绍的tilestaglibs应用的源程序位于配套光盘的 sourcecode/tilestaglibs/version1/tilestaglibs目录下。如果要在Tomcat上发布这个应用,只要把 version1目录下的整个tilestaglibs子目录拷贝到CATALINA_HOME/webapps目录下即可。
图16-2 tilestaglibs应用的主页index.jsp
图16-3 tilestaglibs应用的产品页面product.jsp
例程16-1和例程16-2分别为index.jsp和product.jsp的源代码。
例程16-1 index.jsp
<ccid_nobr>
<ccid_code><%@ 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--%> <td width="150" valign="top" align="left" bgcolor="#CCFFCC"> <table> <tr> <%-- Sidebar top --%> <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 --%> <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> <%-- Main content--%> <td valign="top" height="100%" width="*"> <table width="100%" height="100%"> <tr> <%-- Header--%> <td valign="top" height="15%"> <font size="6">Welcome to ABC Inc.</font> <hr> </td> <tr> <tr> <%-- Content--%> <td valign="top" height="*"> <font size="4">Page-specific content goes here</font> </td> </tr> <tr> <%-- Footer--%> <td valign="bottom" height="15%"> <hr> Thanks for stopping by! </td> </tr> </table> </td> </tr> </table> </body> </html></ccid_code> |
</ccid_nobr>
例程16-2 product.jsp
<ccid_nobr>
<ccid_code><%@ 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--%> <td width="150" valign="top" align="left" bgcolor="#CCFFCC"> <table> <tr> <%-- Sidebar top --%> <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 --%> <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> <%-- Main content--%> <td valign="top" height="100%" width="*"> <table width="100%" height="100%"> <tr> <%-- Header--%> <td valign="top" height="15%"> <font size="6">Welcome to ABC Inc.</font> <hr> </td> <tr> <tr> <%-- Content--%> <td valign="top" height="*"> <font size="4">Products</font> <p> <li>product1</li> <br> <li>product2</li> <br> <li>product3</li> <br> </td> </tr> <tr> <%-- Footer--%> <td valign="bottom" height="15%"> <hr> Thanks for stopping by! </td> </tr> </table> </td> </tr> </table> </body> </html></ccid_code> |
</ccid_nobr>
由例程16-1和例程16-2可以看出,在index.jsp和 product.jsp文件中,仅仅粗体字标识的代码块不是重复代码,其余部分均为重复代码。如果网页的相同部分发生需求变更,必须手工修改所有的JSP 文件。可见,采用基本的JSP语句来编写上述网页,会导致JSP代码的大量冗余,增加开发与维护成本。