SIteMesh介绍

来源:岁月联盟 编辑:exp 时间:2012-05-14

一、SIteMesh介绍
一、SiteMesh简介

SiteMesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。 它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,如htm的内容,使得它的内容也符合你的页面结构的要求。甚至于它能将HTML文件象include那样将该文件作为一个面板的形式嵌入到别的文件中去。所有的这些,都是GOF的Decorator模式的最生动的实现。尽管它是由java语言来实现的,但它能与其他Web应用很好地集成。与传统区别如下图:
 
sitemesh-1-1.jpg (45.97 KB)

2008-9-9 22:40
 
sitemesh-1-2.jpg (53.63 KB)

2008-9-9 22:40

SIteMesh官方地址:http://www.opensymphony.com/sitemesh/index.html
SIteMesh官方下载:http://www.opensymphony.com/sitemesh/download.html
SIteMesh 2.3下载:http://www.javauu.com/downloads/resource/sitemesh-2.3.zip


二、SiteMesh原理

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


三、SiteMesh简单例子

接下来通过一个SiteMesh简单例子来了解SiteMesh的功能:

 

将sitemesh-2.3.jar放 到 [web-app]/WEB-INF/lib目录下;

在[web-app]/WEB-INF/新建一个decorators.xml文件,包含以下内容:普通浏览复制代码打印代码关于程序<?xml version="1.0" encoding="utf-8"?>   <decorators defaultdir="/decorators">       <!-- 此处用来定义不需要过滤的页面 -->       <excludes>       </excludes>       <!-- 用来定义装饰器要过滤的页面 -->       <decorator name="main" page="main.jsp">           <pattern>/*</pattern>       </decorator>   </decorators>  
Xml代码  <?xml version="1.0" encoding="utf-8"?>  <decorators defaultdir="/decorators">      <!-- 此处用来定义不需要过滤的页面 -->      <excludes>      </excludes>     <!-- 用来定义装饰器要过滤的页面 -->      <decorator name="main" page="main.jsp">          <pattern>/*</pattern>      </decorator>  </decorators>  <?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
    <!-- 此处用来定义不需要过滤的页面 -->
    <excludes>
    </excludes>

<!-- 用来定义装饰器要过滤的页面 -->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

在[web-app]/WEB-INF/web.xml添加以下内容:普通浏览复制代码打印代码关于程序<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>  
Xml代码  <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>   <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>

在[web-app]下创建一个decorators文件夹,在该文件下再创建一个装饰页面main.jsp,包含以下内容:普通浏览复制代码打印代码关于程序<%@ page language="java" contentType="text/html; charset=utf-8"       pageEncoding="utf-8"%>   <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   <html>    <!-- 第一个装饰页面 -->       <head>    <!-- 从被装饰页面获取title标签内容,并设置默认值-->    <title><decorator:title default="默认title"/></title>    <!-- 从被装饰页面获取head标签内容 -->           <decorator:head/>       </head>          <body>          <h2>SiteMesh装饰header</h2>          <hr />       <!-- 从被装饰页面获取body标签内容 -->       <decorator:body />          <hr />          <h2>SiteMesh装饰footer</h2>       </body>   </html>  
Html代码  <%@ page language="java" contentType="text/html; charset=utf-8"      pageEncoding="utf-8"%>  <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>   <!-- 第一个装饰页面 -->      <head>   <!-- 从被装饰页面获取title标签内容,并设置默认值-->   <title><decorator:title default="默认title"/></title>   <!-- 从被装饰页面获取head标签内容 -->          <decorator:head/>      </head>        <body>         <h2>SiteMesh装饰header</h2>         <hr />      <!-- 从被装饰页面获取body标签内容 -->      <decorator:body />         <hr />         <h2>SiteMesh装饰footer</h2>      </body>  </html>  <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- 第一个装饰页面 -->
    <head>
<!-- 从被装饰页面获取title标签内容,并设置默认值-->
<title><decorator:title default="默认title"/></title>
<!-- 从被装饰页面获取head标签内容 -->
        <decorator:head/>
    </head>

    <body>
       <h2>SiteMesh装饰header</h2>
       <hr />
    <!-- 从被装饰页面获取body标签内容 -->
    <decorator:body />
       <hr />
       <h2>SiteMesh装饰footer</h2>
    </body>
</html>

在[web-app]下创建被装饰页面index.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</title>    </head>         <body>    <h4>被装饰(目标)页面body标签内内容。</h4>    <h3>使用SiteMesh的好处?</h3>    <ul>        <li>被装饰(目标)页面和装饰页面完全分离。</li>        <li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>         <li>更容易实现统一的网站风格。</li>         <li>还有。。。</li>        </ul>    </body>   </html>  
Html代码  <%@ 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</title>   </head>       <body>   <h4>被装饰(目标)页面body标签内内容。</h4>   <h3>使用SiteMesh的好处?</h3>   <ul>       <li>被装饰(目标)页面和装饰页面完全分离。</li>       <li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>        <li>更容易实现统一的网站风格。</li>        <li>还有。。。</li>       </ul>   </body>  </html>  <%@ 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</title>
</head>

<body>
<h4>被装饰(目标)页面body标签内内容。</h4>
<h3>使用SiteMesh的好处?</h3>
<ul>
     <li>被装饰(目标)页面和装饰页面完全分离。</li>
     <li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>
      <li>更容易实现统一的网站风格。</li>
      <li>还有。。。</li>
     </ul>
</body>
</html>

运行结果如下图:
 
sithmesh-result.jpg (70.96 KB)

2008-9-9 22:40


四、总结

从以上的例子,可以看出通过SiteMesh装饰,不需要在每个目标页面中将header和footer等共同文件include进去,被装饰(目标)页面和装饰页面完全分离。本文只对SiteMesh做一个简单的介绍,SiteMesh可以Velocity,FreeMarker等开源模板工具结合使用,降低页面开发复杂度。

 


摘自 201205083157