活用SiteMesh,一个装饰器就可支撑整个网站结构

来源:互联网 发布:一面 软件 编辑:程序博客网 时间:2024/05/17 02:01

在寻求网站结构的高效统一上,SiteMesh通过Decorator的设计模式,十分利索地达到了目的。其设计思想是,用户发送request至服务器,服务器根据此request生成动态数据,生成网页,准备返回给客户端。就在返回前,SiteMesh进行拦截,对此网页进行解析,将title、body等部分拆解出来,套上模板后,再返回给客户端。由于SiteMesh在返回客户端的最后一步工作,此时的网页已经具备了标准的html网页格式,因此SiteMesh只需解析标准的html网页,无需考虑各个Web应用是应用了JSP、ASP,还是Velocity技术,相当灵活。

一般情况下,我们在decorators.xml文件中定义一个模板main.jsp,来自动套用未加装饰的网页:

<decorators defaultdir="/decorators">
    <decorator name="main" page="main_decorator.jsp">
          <pattern>/*</pattern>
    </decorator>
</decorators>

main_decorator.jsp是默认的装饰器,其pattern应用于所有的网页。其典型的代码如下:

1:  <%@page contentType="text/html"%>
2:  <%@page pageEncoding="gb2312"%>
3:  <%@taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
4:  <%@taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
5:  <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
6: 
7:  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
8:   "http://www.w3.org/TR/html4/loose.dtd">
9:    
10:  <fmt:setBundle basename="AppResource" />
11: 
12:  <html>
13:    <head>
14:      <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
15:      <title><fmt:message key="webapp.name" /> - <decorator:title default="Welcome!" /></title>
16:      <link rel="stylesheet" href="<%=request.getContextPath()%>/css/w3c.css" type="text/css" />
17:      <decorator:head />
18:    </head>
19:    <body id="matrix">
20:      <script type="text/javascript">window.status = "正在加载: <decorator:title default="INTRANET" />...";</script>
21:      <div id="container">
22:        <script type="text/javascript">window.status = "正在加载: 页眉";</script>
23:        <div id='pageHeader'>
24:          <%@include file="/includes/header.jsp"%>
25:        </div>
26:        <div id="centralBody">
27:          <div id="mainbg">
28:            <script type="text/javascript">window.status = "正在加载: 页面内容";</script>
29:            <div id="mainContent">
30:              <decorator:body />
31:            </div>
32:            <script type="text/javascript">window.status = "正在加载: 右边导航栏";</script>
33:            <div id="sideBar">
34:              <page:applyDecorator page="/rootsidebar.jsp" name="sidebar" />
35:            </div>
36:          </div>
37:        </div>
38:        <script type="text/javascript">window.status = "正在加载: 页脚";</script>
39:        <div id="footer">
40:          <%@include file="/includes/footer.jsp"%>
41:        </div>
42:      </div>
43:      <script type="text/javascript">window.status = "完毕";</script>
44:    </body>
45:  </html>

假设有一个未加装饰的网页index.jsp如下:

<%@page contentType="text/html; charset=gb2312"%>
<html>
    <head>
        <title>首页</title>
    </head>
    <body>
        <h2>Hello, World!</h2>
    </body>
</html>

当SiteMesh工作时,它会分解出title及body的内容,并将其分别通过<decorator:title>及<decorator:body>套用到默认的main.jsp上。

好,首页工作得很好,我们准备装饰第二个网页downlaod.jsp。此网页的内容如下:

<%@page contentType="text/html; charset=gb2312"%>
<html>
    <head>
        <title>下载页面</title>
    </head>
    <body>
        <h2>欢迎下载!</h2>
    </body>
</html>

我们发现,当main_decorator.jsp这个装饰器装饰此网页上时,title及body均没问题。但在第34行,作为下载页面的sidebar不应该是rootsidebar,而应是downloadsidebar,即第34行的代码应为:

34:  <page:applyDecorator page="/downloadsidebar.jsp" name="sidebar" />

很显然,我们需要另外一个装饰器,于是我们将第34行改后另存为一个名为download_decorator.jsp的装饰器,还是存于/decorators下面。

相应地,在download.jsp的<title>此行上加上:

  <meta name="decorator" content="download_decorator" />

SiteMesh将根据此元数据的指示,自动调用download_decorator.jsp的装饰器。

但观察两个装饰器,只有第34行不同。只因为一行不同而需要创建另一个装饰器,代价较高。而往下,每一个需要不同的sidebar的页面都必须另行创建一个新装饰器。看来这种方式令人无法忍受。我们注意到,title、body与sidebar都是被装饰的部分,但title及body却可以不用修改装饰器实现共享,而sidebar的装饰部分却不行。title及body均是由<decorator>标签来实现,而sidebar由<page:applyDecorator>来实现。看来不同的结果就源自于这两个标签的不同之上。

我们来看SiteMesh后台的实现原理。SiteMesh在装饰网页时,先分解出index.jsp及download.jsp的title及body,然后读取装饰器,遇到<decorator>标签时,换入title及body的内容。遇到<page:applyDecorator>时,读取其page属性所指定的网页,然后应用名为sidebar的另一个装饰器来装饰sidebar,装饰完后再将结果换进main_decorator。看来,两者的区别在于<decorator>所加工的素材是未装饰的网页已经主动提供的,而因为我们不能主动提供素材给<page:applyDecorator>,因此main_decorator必须自己去找。这就是它们的本质区别。

这使我们想起设计思想中的依赖注入模式。<decorator>就是一个setter,而<page:applyDecorator>则是一个getter,因此它必须自己去寻找所需的素材。如果我们能根据依赖注入模式,为其注入所需素材,那么,就像<decorator>一样,它也不需要再变了。

实现的思路是,download.jsp负责传递一个含有具体page文件名的参数给<page:applyDecorator>,然后,<page:applyDecorator>将其值赋于page属性。对于download.jsp来讲,存放于参数的好地方是网页中的<meta>部分。因此,在其<title>之上加上这一行:

  <meta name="sidebar" content="/downloadsidebar.jsp" />

这样,<page:applyDecorator>就有机会读取此参数了。配合使用SiteMesh的另一个标签<decorator:usePage>,我们可以顺利地为page属性设定值。

  <decorator:usePage id="myPage" />
  <page:applyDecorator page='<%= myPage.getProperty("meta.sidebar") %>' name="sidebar" />

通过这种方法,将原来的由main_decorator主动索取sidebar文件名的方式,改为其被动地接收参数,然后由在各个页面中通过设定meta name="sidebar"的方式,将sidebar文件名传入。从对象责任的角度来看,也正应页面才能知道其sidebar是什么。从而在SiteMesh中成功地应用于依赖注入的模式。

这样,只需一个装饰器,就可以支撑整个网站的结构了。 

原创粉丝点击