`
feixingfei
  • 浏览: 43031 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论
  • calosteward: 感谢分享,我还找到了另一个也是可以用于c#的代码,是一个英文网 ...
    PDF合并

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

 
阅读更多

http://blog.csdn.net/tanghw

在寻求网站结构的高效统一上,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" divfix="decorator"%>
4: <%@taglib uri="http://www.opensymphony.com/sitemesh/page" divfix="page"%>
5: <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" divfix="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中成功地应用于依赖注入的模式。

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

分享到:
评论

相关推荐

    页面装饰器(sitemesh)实例源代码

    用sitemesh页面装饰器,将大名鼎鼎的开源即时通讯服务器openfire中运用的,布局页面抽取出来。MyEclipse中可以直接部署的代码。

    sitemesh

    sitemesh 装饰 母版

    sitemesh装饰器入门

    NULL 博文链接:https://mgxy123.iteye.com/blog/1565892

    sitemesh-3.0.1-javadoc

    SiteMesh是一个网页布局和装饰框架以及Web应用程序集成框架,可帮助创建由页面组成的网站,这些页面需要一致的外观,导航和布局方案。 SiteMesh会拦截对通过Web服务器请求的任何静态或动态生成的HTML页面的请求,...

    Struts2整合SiteMesh技巧

    如果需要自定义装饰器映射器,需要在WEB-INF目录下创建一个sitemesh.xml文件(通常从发布包中拷贝过来更改相应部分)。这一步骤是可选的,通常缺省的配置就能够满足要求。 定义装饰器文件 缺省情况下,sitemesh...

    sitemesh简单教程页面装配器

    d,content,banner 结合为一个完整的视图。通常我们都是用 include 标签在每个 jsp 页面中来 断的包含各种header , stylesheet, scripts and footer,现在,在sitemesh的帮助下,我们可以 心的删掉他们了。...

    JSP布局框架SiteMesh.zip

    SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大 量页面构成的...

    siteMesh demo+文档

    siteMesh demo siteMesh使用文档

    SiteMesh教程及SiteMesh官方文档翻译

    web布局框架 SiteMesh教程及SiteMesh官方文档翻译

    sitemesh-3.0.1.jar

    sitemesh 装饰页面技术.

    sitemesh 完美合集 4个资料和jar文件

    SiteMesh是一个Web页面布局修饰框架, 用于构建包含大量页面, 需要一致的外观样式(look/fell), 导航和布局机制的大型网站. sitemesh应用Decorator模式,用filter截取request和response,把页面组件head,content,...

    siteMesh demo 例子

    入门的demo...siteMesh与freemarker结合。。 siteMesh与velocity结合。。并且在siteMesh.xml用了不同的装饰器。。每个装饰器都有注释。。可以帮助理解。。请看readme.txt

    sitemesh.jar包

    sitemesh.jar包 sitemesh.jar 包sitemesh.jar 包sitemesh.jar包

    sitemesh 例子

    sitemesh 例子,里面举例一个简单例子,而且还有一个简单文档说明

    网络应用程序整合框架SiteMesh源码

    SiteMesh是一款网页布局和装饰器框架,也是一个网络应用程序整合框架,它可以用来维护那些很多页面,并且希望保持所有页面的布局、链接和风格一致的大型网站应用整合与维护。使用SiteMesh可以抽象出页面中的公共布局...

    Sitemesh 3 的使用及配置

    Sitemesh 3 的使用及配置

    siteMesh使用示例

    siteMesh使用示例:登录、注册页面

    sitemesh框架使用 自我整理

    公司的OA流項目視圖層都是採用sitemesh技術,感覺還可以,裝潢頁面,技術不是很複雜,頁面結構清晰,佈局明朗,很好的佈局框架。

    MiddleGen+Sitemesh.zip

    本书使用版本:2.1 官方网站:http://boss.bekk.no/boss/middlegen/ 下载页面:http://sourceforge.net/project/showfiles.php?group_id=36044 下载地址:...

    SiteMesh2.3很全的一个资料

    SiteMesh2.3(含带所需lib并且还有相关文档,实例作参考),天涯精心总结,吐血推荐!

Global site tag (gtag.js) - Google Analytics