111111
精灵王
精灵王
  • 注册日期2010-12-08
  • 发帖数640
  • QQ
  • 火币1103枚
  • 粉丝120
  • 关注75
  • 社区明星
阅读:2959回复:0

使用sitemesh建立复合视图 - 1.hello-JSP教程,J2EE/EJB/服务器

楼主#
更多 发布于:2011-01-08 19:55
sitemesh是opensymphony团队研发的j2ee应用框架之一,旨在提高页面的可维护性和复用性。opensymphony的另一个广为人知的框架为webwork是用作web层的表示框架。他们都是开源的,能在www.sf.net下找到。 应用于以下大项目的例子:http://opensource.thoughtworks.com/projects/sitemesh.html
  • www.jboss.org
  • www.theserverside.com
  • www.opensymphony.com
  • www.atlassian.com
[table=100%][tr][td=1,1,100%]简介: [/td][/tr][tr][td=1,1,100%]sitemesh应用decorator模式,用filter截取request和response,把页面组件head,content,banner结合为一个完整的视图。
通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and footer,目前,
在sitemesh的帮助下,我们能开心的删掉他们了。如下图,你想轻松的达到复合视图模式,那末看完本文吧。
 [/td][/tr][tr][td=1,1,100%]hello sitemesh: [/td][/tr][tr][td=1,1,100%]
  1. 在web-inf/web.xml中copy以下filter的定义:
    [pre]<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>
     <taglib>   <taglib-uri>sitemesh-decorator</taglib-uri>  
     <taglib-location>/web-inf/sitemesh-decorator.tld</taglib-location> </taglib>  
    <taglib>   <taglib-uri>sitemesh-page</taglib-uri>  
    <taglib-location>/web-inf/sitemesh-page.tld</taglib-location> </taglib>
    [/pre]

  2. copy所需jar和dtd文件至相应目录,访问opensymphony.sourceforge.net的cvs以获取sitemesh最新版本。
    sitemesh.jarweb-inf/lib
    sitemesh-decorator.tldweb-inf
    sitemesh-page.tldweb-inf

  3. 建立web-inf/decorators.xml描述各装饰器页面(可仿照sitemesh例子)。
    [pre]<decorators defaultdir="/_decorators">    
    <decorator name="main" page="main.jsp">        
    <pattern>*</pattern>     </decorator>
    </decorators>
    [/pre]


  4. 建立装饰器页面 /_decorators/main.jsp
    [pre]<%@ page contenttype="text/html; charset=gbk"%>
    <%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
     <html>  
     <head>     <title><decorator:title default="装饰器页面..." /></title>
        <decorator:head />  
    </head>  
    <body>     sitemesh的例子<hr>    
    <decorator:body />    
    <hr>chen56@msn.com   </body>
    </html>
    [/pre]

  5. 建立一个的被装饰页面 /index.jsp(内容页面)
    [pre]<%@ page contenttype="text/html; charset=gbk"%>
    <html>  
    <head>     <title>agent test</title>  
    </head>
    <body>     <p>本页只有一句,就是本句.</p>  
    </body>
    </html>
    [/pre]
最后访问index.jsp,将生成如下页面: 而且,所有的页面也会如同index.jsp相同,被sitemesh的filter使用装饰模式修改成如上图般模样,却不用再使用include标签。
[/td][/tr][/table]

更多黑客技术 黑客软件 计算机技术 编程技术 网站技术 qq技术 IT新闻 黑客基地 请访问 灯火安全联盟  灯火黑客 www.hack8888.com/bbs

喜欢0 评分0
游客

返回顶部