`
Cecily
  • 浏览: 57748 次
  • 性别: Icon_minigender_2
  • 来自: 大连
社区版块
存档分类
最新评论

SiteMesh入门事例

阅读更多
Site Mesh是open symphony团队开发的j2ee应用框架之一,是一个用来在JSP中实现页面布局和装饰(layout and decoration)的框架组件,能够帮助网站开发人员较容易实现页面中动态内容和静态装饰外观的分离,旨在提高页面的可维护性和复用性。Site Mesh是开源的,可以在www.sf.net下找到。
           Site mesh应用Decorator模式,用filter截取request和response,把页面组件head ,content ,banner结合为一个完整的视图。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, style sheet, scripts and footer,现在,在site mesh的帮助下,我们可以删除他们了。下面是一个使用SiteMesh实现页面内容的简单事例(参考的是其他网络上的帖子)。需要的文件有:

sitemesh.jar
WEB-INF/lib
sitemesh-decorator.tld
WEB-INF
sitemesh-page.tld
WEB-INF

Web.xml中添加相关内容:
  1. <filter>  
  2.   <filter-name>sitemeshfilter-name>  
  3.   <filter-class>com.opensymphony.module.sitemesh.filter.PageFilterfilter-class>  
  4. filter>  
  5.   
  6. <filter-mapping>  
  7.   <filter-name>sitemeshfilter-name>  
  8.   <url-pattern>/*url-pattern>  
  9. filter-mapping>  
  10.   
  11. <taglib>  
  12.   <taglib-uri>sitemesh-decoratortaglib-uri>  
  13.   <taglib-location>/WEB-INF/sitemesh-decorator.tldtaglib-location>  
  14. taglib>  
  15.   
  16. <taglib>  
  17.   <taglib-uri>sitemesh-pagetaglib-uri>  
  18.   <taglib-location>/WEB-INF/sitemesh-page.tldtaglib-location>  
  19. taglib>  

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

  1. <decorators defaultdir="/_decorators">   
  2.  <decorator name="main" page="main.jsp">        
  3.   <pattern>*pattern>    
  4.  decorator>
  5. decorators>  
    各标签常见属性的含义为:
    • defaultdir: 包含装饰器页面的目录
    • page : 页面文件名
    • name : 别名
    • role : 角色,用于安全
    • webapp : 可以另外指定此文件存放目录
    • Patterns : 匹配的路径,可以用*,那些被访问的页面需要被装饰。
    建立装饰器页面 /_decorators/main.jsp
    1. <%@ page contentType="text/html; charset=GBK"%>
    2. <%@ taglib uri="sitemesh-decorator" prefix="decorator" %><html>  
    3. <head><META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gbk">    
    4. <title><decorator:title default="装饰器页面..." />title> 
    5.   <decorator:head />  
    6. head>  
    7. <body>    sitemesh example :Header<hr>    
    8. <decorator:body />   
    9.  <hr>sitemesh example:Footer
    10. body>
    11. html>  
    1. <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gbk">解决中文问题。  

    建立一个的被装饰页面 /index.jsp(测试页面)     

    1. <%@ page contentType="text/html; charset=GBK"%>
    2. <html> 
    3.  <head>  
    4.   <title>Agent Testtitle>
    5.  head>  <body>  
    6.   <p>My Test Body.<p>  
    7. >
    8. html>  

    <o:p>运行程序即可。对SiteMesh还不是很熟悉,望多指正。<o:p></o:p></o:p>

分享到:
评论
1 楼 qianlei007 2007-04-23  
学习下....
    看例子,还是挺简单的...

相关推荐

Global site tag (gtag.js) - Google Analytics