sitemesh opensymphony 团队开发的 j2ee 应用框架之一,旨在提高页面的可维护性和复用性。 opensymphony 的另一个广为人知的框架为 webwork 是用作 web 层的表示框架。他们都是开源的,可以在 www.sf.net 下找到。 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

应用于以下大项目的例子: http://opensource.thoughtworks.com/projects/sitemesh.html

简介:

sitemesh 应用 Decorator 模式,用 filter 截取 request response, 把页面组件 head,

content,banner 结合为一个完整的视图。

通常我们都是用 include 标签在每个 jsp 页面中来不断的包含各种 header, stylesheet,

scripts and footer ,现在,在 sitemesh 的帮助下,我们可以开心的删掉他们了。

如下图,你想轻松的达到复合视图模式,那末看完本文吧。

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

  

hello sitemesh

1.     WEB-INF/web.xml copy 以下 filter 的定义 :

<filter>

     <filter-name>sitemesh</filter-name>

     <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>

</filter>

 

<filter-mapping>

     <filter-name>sitemesh</filter-name>

     <url-pattern>/*</url-pattern>

</filter-mapping>

 

<taglib>

     <taglib-uri>sitemesh-decorator</taglib-uri>

     <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>

</taglib>

 

<taglib>

     <taglib-uri>sitemesh-page</taglib-uri>

     <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>

</taglib>

 

2.     copy 所需 jar dtd 文件至相应目录,访问 opensymphony.sourceforge.net cvs 以获取 sitemesh 最新版本。

sitemesh.jar

WEB-INF/lib

sitemesh-decorator.tld

WEB-INF

sitemesh-page.tld

WEB-INF

3.    

 

4.     建立 WEB-INF/decorators.xml 描述各装饰器页面 ( 可仿照 sitemesh 例子 )

<decorators defaultdir="/_decorators">

       <decorator name="main" page="main.jsp">

           <pattern>*</pattern>

       </decorator>

</decorators>

5.    

 

6.     建立装饰器页面 /_decorators/main.jsp

<%@ page contentType="text/html; charset=GBK"%>

<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>

 

<html>

     <head>

       <title><decorator:title default=" 装饰器页面..." /></title>

       <decorator:head />

     </head>

     <body>

       sitemesh 的例子<hr>

       <decorator:body />

       <hr>chen56@msn.com

     </body>

</html>

 

7.     建立一个的被装饰页面 /index.jsp( 内容页面 )

<%@ page contentType="text/html; charset=GBK"%>

<html>

     <head>

       <title>Agent Test</title>

     </head>

     <body>

       <p> 本页只有一句,就是本句.</p>

     </body>

</html>

最后访问 index.jsp ,将生成如下页面:

而且,所有的页面也会如同 index.jsp 一样,被 sitemesh filter 使用装饰模式修改成如

上图般模样,却不用再使用 include 标签。

 

装饰器      decorator 概念

建立可复用的 web 应用程序 , 一个通用的方法是建立一个分层系统,如同下面一个普通的 web 应用:

  • 前端,front-end:JSPServlets,或jakartavelocity

  • 控制层框架 Controller (Struts/Webwork)

  • 业务逻辑 Business :主要业务逻辑

  • 持久化框架 hibernate/jdo

可糟糕的是前端的页面逻辑很难被复用,当你在每一个页面中用数之不尽的 include 来复用公共的 header, stylesheet, scripts footer 时,一个问题出现了 - 重复的代码,每个页面必须用 copy 来复用页面结构,而当你需要创意性的改变页面结构时,灾难就爱上了你。

sitemesh 通过 filter 截取 request response ,并给原始的页面加入一定的装饰 ( 可能为 header,footer...) ,然后把结果返回给客户端,并且被装饰的原始页面并不知道 sitemesh 的装饰,这也就达到了脱耦的目的。

据说即将新出台的 Portlet 规范会帮助我们标准的实现比这些更多更 cool 的想法,但可怜的我还不懂它到底是一个什末东东,有兴趣的人可以研究
jetspeed
,或 JSR (Java Specification Request) 168, 但我想 sitemesh 如此简单,我们不妨先用着。

 

让我们看看怎样配置环境

除了要 copy WEB-INF/lib 中的 sitemesh.jar, copy WEB-INF 中的 sitemesh-decorator.tld,sitemesh-page.tld 文件外,还有 2 个文件要建立到 WEB-INF/

  • sitemesh.xml (可选)  

  • decorators.xml

sitemesh.xml 可以设置 2 种信息 :

Page Parsers :负责读取 stream 的数据到一个 Page 对象中以被 SiteMesh 解析和操作。 ( 不太常用,默认即可 )

Decorator Mappers : 不同的装饰器种类,我发现 2 种比较有用都列在下面。一种通用的 mapper, 可以指定装饰器的配置文件名,另一种可打印的装饰器,可以允许你当用 http://localhost/aaa/a.html?printable=true 方式访问时给出原始页面以供打印 ( 免得把 header,footer 等的花哨的图片也搭上 )

(
但一般不用建立它,默认设置足够了: com/opensymphony/module/sitemesh/factory/sitemesh-default.xml ):

范例:

<sitemesh>
     <page-parsers>
       <parser default="true" class="com.opensymphony.module.sitemesh.parser.DefaultPageParser" />
       <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
       <parser content-type="text/html;charset=ISO-8859-1" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
     </page-parsers>

     <decorator-mappers>
       <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
         <param name="config" value="/WEB-INF/decorators.xml" />
       </mapper>
         <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
            <param name="decorator" value="printable" />
            <param name="parameter.name" value="printable" />
                    <param name="parameter.value" value="true" />
         </mapper>

  </decorator-mappers>
</sitemesh>

decorators.xml :定义构成复合视图的所有页面构件的描述 ( 主要结构页面, header,footer...) ,如下例:

<decorators defaultdir="/_decorators">
     <decorator name="main" page="main.jsp">
       <pattern>*</pattern>
     </decorator>
     <decorator name="printable" page="printable.jsp" role="customer" webapp="aaa" />
</decorators>

  • defaultdir: 包含装饰器页面的目录

  • page : 页面文件名

  • name : 别名

  • role : 角色,用于安全

  • webapp : 可以另外指定此文件存放目录

  • Patterns : 匹配的路径,可以用*,那些被访问的页面需要被装饰。