web开发当中,前端的页面逻辑很难被重用,当我们在每一个页面中用include来复用公共的header, css, js,footer时,会大量的出现重复的代码,无形中增加的开发人员的负担.sitemesh通过filter截取request和response,并给原始的页面加入一定的装饰(可能为header,footer...),然后把结果返回给客户端,并且被装饰的原始页面并不知道sitemesh的装饰,这也就达到了脱耦的目的。
SiteMesh 是opensymphony项目,下是官网中的介绍:
SiteMesh is a web-page layout and decoration framework and web- application integration framework to aid in creating large sites consisting of many pages for which a consistent look/feel, navigation and layout scheme is required.

SiteMesh intercepts requests to any static or dynamically generated HTML page requested through the web-server, parses the page, obtains properties and data from the content and generates an appropriate final page with modifications to the original. This is based upon the well-known GangOfFour Decorator design pattern.

SiteMesh can also include entire HTML pages as a Panel within another page. This is similar to a Server-Side Include, except that the HTML document will be modified to create a visual window (using the document's Meta-data as an aid) within a page. Using this feature, Portal type web sites can be built very quickly and effectively. This is based upon the well-known GangOfFour Composite design pattern.

SiteMesh is built using Java 2 with Servlet, JSP and XML technologies. This makes it ideal for use with J2EE applications, however it can be integrated with server-side web architectures that are not Java based such as CGI (Perl/Python/C/C++/etc), PHP, Cold Fusion, etc...

SiteMesh is very extensible and is designed in a way in which it is easy to extend for custom needs.

WEB-INF  下decorator.xml文件
  1. <decorators defaultdir="/WEB-INF/decorators">   
  2.     <!-- 不需要修饰的页面 -->   
  3.     <excludes>   
  4.         <pattern>/css/*</pattern>   
  5.         <pattern>/js/*</pattern>   
  6.         <pattern>/p_w_picpaths/*</pattern>   
  7.         <pattern>/dojo/*</pattern>   
  8.         <pattern>/webwork/*</pattern>   
  9.         <pattern>/login.jsp*</pattern>   
  10.          <pattern>/register/*</pattern>   
  11.     </excludes>   
  12.     <decorator name="main" page="main.jsp">   
  13.         <pattern>/*</pattern>   
  14.     </decorator>   
  15.      </decorators>  
defaultdir为装饰页面所在的文件夹.

装饰页面main.jsp,主要的页面结构布局.
代码:
  1. <%@ page contentType="text/html;charset=utf-8" language="java"%>   
  2. [color=red]<%@ taglib uri="sitemesh-decorator" prefix="decorator"%>   
  3. <%@ taglib uri="sitemesh-page" prefix="page"%>[/color]   
  4.   
  5. <%   
  6. String path = request.getContextPath();   
  7. %>   
  8. <HTML>   
  9.     <HEAD>   
  10.     <TITLE><decorator:title default="main page" /></TITLE>   
  11.   
  12.         <decorator:head />   
  13.     <link rel="stylesheet" type="text/css"  
  14.             href="<%=path%>/css/default.css" />   
  15.         <link rel="stylesheet" type="text/css" href="<%=path%>/css/tab.css" />   
  16.         <script language="javascript" src="<%=path%>/js/formControl.js"></script>   
  17.         <script language="javascript" src="<%=path%>/js/changePage.js"></script>   
  18.         <META http-equiv=ImageToolbar content=no>   
  19.     </HEAD>   
  20.     <BODY id=main>   
  21.         <jsp:include flush="true" page="/commont/header.jsp"></jsp:include>   
  22.         <DIV id=container>   
  23.             <DIV id=content style="height:500px">   
  24.                 <decorator:body />            <DIV id=navigation>   
  25.                     <A accessKey=3 name=menu></A>   
  26.                     <H2 class=hide>   
  27.                         Navigation   
  28.                     </H2>   
  29.                     <UL id=menuroot>   
  30.                         <LI>   
  31.                             <A title="index" accessKey=1 href="<%=path%>/index.jsp">Index</A>   
  32.                         </LI>   
  33.                         <LI>   
  34.                             <A title="ListUser" accessKey=3  
  35.                                 href="<%=path%>/user/listUsers.action">ListUser</A>   
  36.                         </LI>   
  37.                         <LI>   
  38.                             <A title="listWorkSum" accessKey=4  
  39.                                 href="<%=path%>/worksum/listWorkSums.action">listWorkSum</A>   
  40.                         </LI>   
  41.     <LI>   
  42.                             <A title="ListUser" accessKey=3  
  43.                                 href="<%=path%>/clickstream/clickstreams.jsp">ClickStream</A>   
  44.                         </LI>   
  45.                         <LI>   
  46.                             <A title="monitor" accessKey=6  
  47.                                 href="<%=path%>/monitor/jamonadmin.jsp">monitor</A>   
  48.                         </LI>   
  49.                         <LI>   
  50.                             <A title="workflow" accessKey=6  
  51.                                 href="<%=path%>/workflow/default.jsp">workflow</A>   
  52.                         </LI>   
  53.                         <LI>   
  54.                             <A title="workflow" accessKey=4  
  55.                                 href="<%=path%>/workflow/workflowLogin.action">workflowAction</A>   
  56.                         </LI>   
  57.                         <LI>   
  58.                             <A title="soap" accessKey=6  
  59.                                 href="<%=path%>/soap/default.jsp">soap</A>   
  60.                         </LI>   
  61.                         <LI>   
  62.                             <A title="Logout" accessKey=5 href="<%=path%>/logout.jsp">Logout</A>   
  63.                         </LI>   
  64.                     </UL>   
  65.                 </DIV>   
  66.   
  67.   
  68.             </DIV>   
  69.             <DIV id=header>   
  70.                 Copyright © 2003-2005 cherubo All Rights Reserved   
  71.             </DIV>   
  72.             <jsp:include page="/commont/footer.jsp" />   
  73.     </BODY>   
  74. </HTML>  
上面页面
以下列着全部标签: Decorator Tags Page Tags
被用于建立装饰器页面. 被用于从原始内容页面访问装饰器.
<decorator:head />
<decorator:body />
<decorator:title />
<decorator:getProperty />
<decorator:usePage />
<page:applyDecorator />
<page:param  
<decorator:head />
插入原始页面(被包装页面)的head标签中的内容(不包括head标签本身)。
<decorator:body />
插入原始页面(被包装页面)的body标签中的内容。
<decorator:title [ default="..." ] />
插入原始页面(被包装页面)的title标签中的内容,还可以添加一个缺省值。

被修饰的页面
  1. <html>   
  2. <head>   
  3.     <title>main</title>    
  4.   
  5.        
  6. </head>   
  7. <body>   
  8. <div style="PADDING-TOP: 50px;">   
  9. <h1>   
  10. Welcome Into NewiKi System    
  11. </h1>   
  12. <h3>In Newiki System,You Can:</h3>   
  13. <h3>You can do Anything!</h3>   
  14. <h3>It's Life ,Live It!</h3>   
  15. <h3>You Are Freedom!</h3>   
  16.   
  17. </div>   
  18.  </body>   
  19. </html>  
很简单,很清楚的结构.

WEB.XML
  1. <filter>   
  2.     <filter-name>sitemesh</filter-name>   
  3.        <filter-class>   
  4.            com.opensymphony.module.sitemesh.filter.PageFilter   
  5.        </filter-class>   
  6.     </filter>   
  7.         <filter-mapping>   
  8.         <filter-name>sitemesh</filter-name>   
  9.         <url-pattern>*.jsp</url-pattern>   
  10.     </filter-mapping>   
  11.     <filter-mapping>   
  12.         <filter-name>sitemesh</filter-name>   
  13.         <url-pattern>*.action</url-pattern>   
  14.     </filter-mapping>