1.介绍
SiteMesh的是应用Decorator模式,以允许从表现内容的完全分离一个轻量级的,灵活的Java Web应用程序框架。
siteMesh框架是OpenSymphony团队开发的一个非常优秀的页面装饰器框架,它通过对用户请求进行过滤,并对服务器向客户端响应也进行过滤,然后给原始页面加入一定的装饰(header,footer等),然后把结果返回给客户端。通过SiteMesh的页面装饰,可以提供更好的代码复用,所有的页面装饰效果耦合在目标页面中,无需再使用include指令来包含装饰效果,目标页与装饰页完全分离,如果所有页面使用相同的装饰器,可以是整个Web应用具有统一的风格。

步骤1
引入JAR包sitemesh-3.0.1.jar
步骤2
WEB-INF目录下创建sitemesh3.xml
<?xml version="1.0" encoding="UTF-8"?> <sitemesh> <!-- 指明满足“/*”的页面,将被“/master.jsp”所装饰 --> <mapping path="/*" decorator="/master.jsp" /> <!-- 指明满足“/exclude.jsp*”的页面,将被排除,不被装饰 --> <mapping path="/el.jsp*" exclue="true" /> </sitemesh>
步骤3
WEB-INF目录下web.xml中加入sitemesh
 <filter>  
    <filter-name>sitemesh</filter-name>  
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>  
  </filter>  
  
  <filter-mapping>  
    <filter-name>sitemesh</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>  
步骤4
建立母版页面master.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><sitemesh:write property=‘title‘/></title> <sitemesh:write property=‘head‘/> </head> <body> <h1 class=‘title‘>SiteMesh example site: <sitemesh:write property=‘title‘/></h1> <hr> <sitemesh:write property=‘body‘/> </body> </html>
步骤5
建立测试页面 test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>这是测试</title> </head> <body> <p>sitemesh应用Decorator模式,用filter截取request和response,把页面组件head,content,banner结合为一个完整的视图。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet,scripts and footer,现在,在sitemesh的帮助下,我们可以开心的删掉他们了。如下图,你想轻松的达到复合视图模式,那末看完本文吧。</p> </body> </html>
步骤6 完成,效果如下

sitemesh3.xml 配置详解
<sitemesh>
    <!--默认情况下,
        sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰,
        我们可以添加更多的 mime 类型-->
  <mime-type>text/html</mime-type>
  <mime-type>application/vnd.wap.xhtml+xml</mime-type>
  <mime-type>application/xhtml+xml</mime-type>
  ...
  
  <!-- 默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰 -->
  <mapping decorator="/default-decorator.html"/>
  
  <!-- 对不同的路径,启用不同的装饰器 -->
  <mapping path="/admin/*" decorator="/another-decorator.html"/>
  <mapping path="/*.special.jsp" decorator="/special-decorator.html"/>
  <!-- 对同一路径,启用多个装饰器 -->
  <mapping>
    <path>/articles/*</path>
    <decorator>/decorators/article.html</decorator>
    <decorator>/decorators/two-page-layout.html</decorator>
    <decorator>/decorators/common.html</decorator>
  </mapping>
  <!-- 排除,不进行装饰的路径 -->
  <mapping path="/javadoc/*" exclue="true"/>
  <mapping path="/brochures/*" exclue="true"/>
  
  <!-- 自定义 tag 规则 -->
  <content-processor>
    <tag-rule-bundle class="com.something.CssCompressingBundle" />
    <tag-rule-bundle class="com.something.LinkRewritingBundle"/>
  </content-processor>
  ...
</sitemesh>
原文:http://www.cnblogs.com/jinchangshun/p/4656501.html