SIteMesh介绍

转自:http://javauu.com/thread-27-1-1.html

一、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文件,包含以下内容:
    普通浏览 复制代码 打印代码 关于程序
    1. <?xml version="1.0" encoding="utf-8"?> 
    2. <decorators defaultdir="/decorators"> 
    3.     <!-- 此处用来定义不需要过滤的页面 --> 
    4.     <excludes> 
    5.     </excludes> 
    6.  
    7. <!-- 用来定义装饰器要过滤的页面 --> 
    8.     <decorator name="main" page="main.jsp"> 
    9.         <pattern>/*</pattern> 
    10.     </decorator> 
    11. </decorators> 
    Xml代码 复制代码 收藏代码
    1. <?xml version="1.0" encoding="utf-8"?> 
    2. <decorators defaultdir="/decorators"> 
    3.     <!-- 此处用来定义不需要过滤的页面 --> 
    4.     <excludes> 
    5.     </excludes> 
    6.  
    7. <!-- 用来定义装饰器要过滤的页面 --> 
    8.     <decorator name="main" page="main.jsp"> 
    9.         <pattern>/*</pattern> 
    10.     </decorator> 
    11. </decorators> 
  • 在[web-app]/WEB-INF/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.  
    8. <filter-mapping> 
    9. <filter-name>sitemesh</filter-name> 
    10. <url-pattern>/*</url-pattern> 
    11. </filter-mapping> 
    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.  
    8. <filter-mapping> 
    9. <filter-name>sitemesh</filter-name> 
    10. <url-pattern>/*</url-pattern> 
    11. </filter-mapping> 
  • 在[web-app]下创建一个decorators文件夹,在该文件下再创建一个装饰页面main.jsp,包含以下内容:
    普通浏览 复制代码 打印代码 关于程序
    1. <%@ page language="java" contentType="text/html; charset=utf-8" 
    2.     pageEncoding="utf-8"%> 
    3. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%> 
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    5. <html> 
    6. <!-- 第一个装饰页面 --> 
    7.     <head> 
    8. <!-- 从被装饰页面获取title标签内容,并设置默认值--> 
    9. <title><decorator:title default="默认title"/></title> 
    10. <!-- 从被装饰页面获取head标签内容 --> 
    11.         <decorator:head/> 
    12.     </head> 
    13.  
    14.     <body> 
    15.        <h2>SiteMesh装饰header</h2> 
    16.        <hr /> 
    17.     <!-- 从被装饰页面获取body标签内容 --> 
    18.     <decorator:body /> 
    19.        <hr /> 
    20.        <h2>SiteMesh装饰footer</h2> 
    21.     </body> 
    22. </html> 
    Html代码 复制代码 收藏代码
    1. <%@ page language="java" contentType="text/html; charset=utf-8" 
    2.     pageEncoding="utf-8"%> 
    3. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%> 
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    5. <html> 
    6. <!-- 第一个装饰页面 --> 
    7.     <head> 
    8. <!-- 从被装饰页面获取title标签内容,并设置默认值--> 
    9. <title><decorator:title default="默认title"/></title> 
    10. <!-- 从被装饰页面获取head标签内容 --> 
    11.         <decorator:head/> 
    12.     </head> 
    13.  
    14.     <body> 
    15.        <h2>SiteMesh装饰header</h2> 
    16.        <hr /> 
    17.     <!-- 从被装饰页面获取body标签内容 --> 
    18.     <decorator:body /> 
    19.        <hr /> 
    20.        <h2>SiteMesh装饰footer</h2> 
    21.     </body> 
    22. </html> 
  • 在[web-app]下创建被装饰页面index.jsp,包含以下内容:
    普通浏览 复制代码 打印代码 关于程序
    1. <%@ page language="java" contentType="text/html; charset=utf-8" 
    2.     pageEncoding="utf-8"%> 
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    4. <html> 
    5. <!-- 第一个被装饰(目标)页面  --> 
    6. <head> 
    7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    8. <title>被装饰(目标)页面title</title> 
    9. </head> 
    10.    
    11. <body> 
    12. <h4>被装饰(目标)页面body标签内内容。</h4> 
    13. <h3>使用SiteMesh的好处?</h3> 
    14. <ul> 
    15.      <li>被装饰(目标)页面和装饰页面完全分离。</li> 
    16.      <li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li> 
    17.       <li>更容易实现统一的网站风格。</li> 
    18.       <li>还有。。。</li> 
    19.      </ul> 
    20. </body> 
    21. </html> 
    Html代码 复制代码 收藏代码
    1. <%@ page language="java" contentType="text/html; charset=utf-8" 
    2.     pageEncoding="utf-8"%> 
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    4. <html> 
    5. <!-- 第一个被装饰(目标)页面  --> 
    6. <head> 
    7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    8. <title>被装饰(目标)页面title</title> 
    9. </head> 
    10.   
    11. <body> 
    12. <h4>被装饰(目标)页面body标签内内容。</h4> 
    13. <h3>使用SiteMesh的好处?</h3> 
    14. <ul> 
    15.      <li>被装饰(目标)页面和装饰页面完全分离。</li> 
    16.      <li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li> 
    17.       <li>更容易实现统一的网站风格。</li> 
    18.       <li>还有。。。</li> 
    19.      </ul> 
    20. </body> 
    21. </html> 
  • 运行结果如下图:
    sithmesh-result.jpg (70.96 KB)
    2008-9-9 22:40


四、总结
从以上的例子,可以看出通过SiteMesh装饰,不需要在每个目标页面中将header和footer等共同文件include进去,被装饰(目标)页面和装饰页面完全分离。本文只对SiteMesh做一个简单的介绍,SiteMesh可以Velocity,FreeMarker等开源模板工具结合使用,降低页面开发复杂度。
本文例子工程SiteMeshDemoL1代码下载:
SiteMeshDemoL1.rar (153.96 KB)
SiteMeshDemoL1.rar (153.96 KB)
下载次数: 0
2008-9-9 22:40

你可能感兴趣的:(SIteMesh介绍)