Chinaunix首页 | 论坛 | 博客
  • 博客访问: 735870
  • 博文数量: 60
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 2090
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-27 12:33
文章分类

全部博文(60)

文章存档

2008年(60)

我的朋友

分类: Java

2008-04-21 13:03:33

 

    为了减少代码的冗余,可以把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

Welcome to ABC Inc.

    例程16-4 footer.jsp


Thanks for stopping by!

    例程16-5 sidebar.jsp

<%@ page contentType="text/html; charset=UTF-8" %>

      <%-- Sidebar top component --%>
      
      <%-- Sidebar bottom component --%>
      
Links

Home
Products
Hot Link1
Hot Link2
Hot Link3

    例程16-6 indexContent.jsp

Page-specific content goes here

    例程16-7 productContent.jsp

Products 

  • product1

  • product2

  • product3

  •     例程16-8 index.jsp

    <%@ page contentType="text/html; charset=UTF-8" %>
    
       
          TilesTaglibs Sample
       
       
          <%-- One table lays out all of the content for this page --%>
          
                <%-- Sidebar section --%>
                
                <%-- Main content section --%>
                
    <%-- Header section --%> <%-- Content section --%> <%-- Footer section --%>

        例程16-9 product.jsp

    <%@ page contentType="text/html; charset=UTF-8" %>
    
       
          TilesTaglibs Sample
       
       
          <%-- One table lays out all of the content for this page --%>
          
                <%-- Sidebar section --%>
                
                <%-- Main content section --%>
                
    <%-- Header section --%> <%-- Content section --%> <%-- Footer section --%>

        采用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个文件,所以软件的复杂度也增加了。

    阅读(1114) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~