无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

SiteMesh 入门实例

Posted on 2010-05-11 21:45 Gavin.lee 阅读(1163) 评论(0)  编辑  收藏 所属分类: SiteMesh 页面装饰组件

一、SiteMesh设计思想

用户发送request至服务器,服务器根据此request生成动态数据,生成网页,准备返回给客户端。就在返回前,SiteMesh进行拦截,对此网页进行解析,将title、body等部分拆解出来,套上模板后,再返回给客户端。由于SiteMesh在返回客户端的最后一步工作,此时的网页已经具备了标准的html网页格式,因此SiteMesh只需解析标准的html网页,无需考虑各个Web应用是应用了JSP、ASP,还是Velocity技术,相当灵活。
    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简单部署到实现

配置:
除了要copy到WEB-INF/lib中的sitemesh.jar外,还有2个文件要建立到WEB-INF/:
sitemesh.xml (可选)  
decorators.xml
sitemesh.xml 可以设置2种信息:
1.Page Parsers :负责读取stream的数据到一个Page对象中以被SiteMesh解析和操作。(不太常用,默认即可)
2.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> 


实现:
这里定义了一个过滤器.所有的请求都交由sitemesh来处理,在web.xml中加入如下片段:

<filter>   
    
<filter-name>sitemeshfilter-name>   
    
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilterfilter-class>   
<filter>   
   
<filter-mapping>   
    
<filter-name>sitemeshfilter-name>   
    
<url-pattern>/*url-pattern>   
<filter-mapping>   
   
<taglib>   
    
<taglib-uri>sitemesh-decoratortaglib-uri>   
    
<taglib-location>/WEB-INF/sitemesh-decorator.tldtaglib-location>   
<taglib>   
   
<taglib>   
    
<taglib-uri>sitemesh-pagetaglib-uri>   
    
<taglib-location>/WEB-INF/sitemesh-page.tldtaglib-location>   
<taglib>

 
建立WEB-INF/decorators.xml描述各装饰器页面。

<decorators defaultdir="/_decorators">  
    
<decorator name="main" page="main.jsp">         
        
<pattern>*pattern>     
    
<decorator>
    
    
<!-- 不需要装饰的目录 -->
    
<excludes>
        
<pattern>/public/*</pattern>
    
</excludes> 
    
    
<!-- 需要装饰的目录 -->
    
<decorator name="inside" page="inside.jsp" role="custom" webapp="client">
        
<pattern>/admin/*</pattern>
        
<pattern>/user/*</pattern>
    
</decorator>
<decorators>

各标签常见属性的含义为:
defaultdir: 包含装饰器页面的目录
page : 页面文件名
name : 别名
role : 角色,用于安全
webapp : 可以另外指定此文件存放目录
Patterns : 匹配的路径,可以用*,那些被访问的页面需要被装饰


 在web下面建一个文件夹取名decorators.在decoratots下面创建上面定义的模板页面main.jsp,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
<head>
       
<title><decorator:title default="默认title" /></title>
    
<body>
        
<h2>SiteMesh装饰header</h2>        
        
<p>Add head decorator</p>
        
<decorator:body />    
        
<p>Add foot decorator</p>
        
<h2>SiteMesh装饰footer</h2>
    
</body>
</html>

说明:

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>

此处为decorator标签的声明。因为我们下面要使用到它

<decorator:title />

把请求的原始页面的title内容插入到<title></title>,比如我们要请求index.jsp页面的时候。会把index.jsp中的title的内容放入到这里

<decorator:body />


把请求的原始页面的body内容插入到<body></body>,发现没有我们在这句的前面加上了<p>Add head decorator...</p>和<p>Add foot decorator...</p>

相当于给我们请求的页面的body内容加上了头部和尾部.实现了模板功能。


在WEB-INF下创建我们要请求访问的页面index.jsp,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
<head>
       
<title>SiteMesh Sample Site</title>
    
</head>
    
<body>
       Welcome to the SiteMesh sample
    
</body>
</html>

把web工程部署到tomcat容器中。

输入http://localhost:8080/SitemeshSample/index.jsp


页面效果如下:

Add head decorator
Welcome to the SiteMesh sample 
Add foot decorator


不难发现,我们index.jsp中只有Welcome to the SiteMesh sample... 一句。但是在返回给我们之前套上了main.jsp模板页。在它的前面和后面分别加上了一句话。通过Sitemesh我们可以很容易实现页面中动态内容和静态装饰外观的分离。


只有注册用户登录后才能发表评论。


网站导航: