轉:SiteMesh簡介

      OS(OpenSymphony)的SiteMesh是一個用來在JSP中實現頁面佈局和裝飾(layout and decoration)的框架組件,可以幫助網站開發人員較容易實現頁面中動態內容和靜態裝飾外觀的分離。
      Sitemesh是由一個基於Web頁面佈局、裝飾以及與現存Web應用整合的框架。它能幫助咱們在由大量頁面構成的項目中建立一致的頁面佈局和外觀,如 一致的導航條,一致的banner,一致的版權,等等。它不只僅能處理動態的內容,如jsp,php,asp等產生的內容,它也能處理靜態的內容,如 htm的內容,使得它的內容也符合你的頁面結構的要求。甚至於它能將HTML文件象include那樣將該文件做爲一個面板的形式嵌入到別的文件中去。所 有的這些,都是GOF的Decorator模式的最生動的實現。儘管它是由java語言來實現的,但它能與其餘Web應用很好地集成。官方:http://www.opensymphony.com/sitemesh/php

 

原文連接:http://blog.csdn.net/fox_lht/article/details/7423968html

 

Web.xml中的寫法:前端

 

[html]  view plain copy
 
  1. <!-- Sitemesh filter -->  
  2.     <filter>  
  3.         <filter-name>sitemeshFilter</filter-name>  
  4.         <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>  
  5.     </filter>  
  6.     <filter-mapping>  
  7.         <filter-name>sitemeshFilter</filter-name>  
  8.         <url-pattern>/*</url-pattern>  
  9.     </filter-mapping>  

配置文件:java

 

[html]  view plain copy
 
  1. <sitemesh>  
  2.   <mapping path="/*" decorator="/decorator.html"/>  
  3. </sitemesh>  



 

decorator文件的寫法web

 

 

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <decorators defaultdir="/WEB-INF/layouts/">  
  3.     <excludes>  
  4.         <pattern>/static/*</pattern>  
  5.     </excludes>  
  6.       
  7.     <decorator name="default" page="default.jsp">  
  8.         <pattern>/*</pattern>  
  9.     </decorator>  
  10. </decorators>  


excludes標籤中的內容是不須要裝飾的目錄或文件。瀏覽器

 

裝飾模板網頁文件的寫法:安全

 

[html]  view plain copy
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <title>Mini-Web示例:<sitemesh:title/></title>  
  5. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />  
  6. <meta http-equiv="Cache-Control" content="no-store" />  
  7. <meta http-equiv="Pragma" content="no-cache" />  
  8. <meta http-equiv="Expires" content="0" />  
  9.   
  10. <sitemesh:head/>  
  11. </head>  
  12.   
  13. <body>  
  14.     <div class="container">  
  15.         <%@ include file="/WEB-INF/layouts/header.jsp"%>  
  16.         <div id="content" class="span-24 last">  
  17.                 <sitemesh:body/>  
  18.         </div>  
  19.         <%@ include file="/WEB-INF/layouts/footer.jsp"%>  
  20.     </div>  
  21. </body>  
  22. </html>  

 

填入到模板文件中網頁:app

 

[html]  view plain copy
 
  1. <%@ page contentType="text/html; charset=GBK"%>  
  2. <html>  
  3.      <head>  
  4.        <title>Agent Test</title>  
  5.      </head>  
  6.      <body>  
  7.        <p>本頁只有一句,就是本句.</p>  
  8.      </body>  
  9. </html>  


SiteMesh介紹:框架

 

 

簡介:
    sitemesh應用Decorator模式,用filter截取request和response,把頁面組件head,content,banner結合爲一個完整的視圖。一般咱們都是用include標籤在每一個jsp頁面中來不斷的包含各類header, stylesheet, scripts and footer,如今,在sitemesh的幫助下,咱們能夠開心的刪掉他們了。以下圖,你想輕鬆的達到複合視圖模式,那末看完本文吧。

1、在WEB-INF/web.xml中copy如下filter的定義:

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">webapp

<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>

2、copy所需sitemesh-2.3.jar到WEB-INF\lib下。(這裏能夠下載http://www.opensymphony.com/sitemesh/)

3、 創建WEB-INF/decorators.xml描述各裝飾器頁面。 

<decorators defaultdir="/decorators">
       <decorator name="main" page="main.jsp">
           <pattern>*</pattern>
       </decorator>
</decorators>

  上面配置文件指定了裝飾器頁面所在的路徑,並指定了一個名爲main的裝飾器,該裝飾器默認裝飾web應用根路徑下的全部頁面。

4、 創建裝飾器頁面 /decorators/main.jsp

  • <%@ page contentType="text/html; charset=GBK"%>
    <%@ taglib uri="http://www.opensymphony.com/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>
     

    5、創建一個的被裝飾頁面 /index.jsp(內容頁面)

  • <%@ page contentType="text/html; charset=GBK"%>
    <html>
         <head>
           <title>Agent Test</title>
         </head>
         <body>
           <p>本頁只有一句,就是本句.</p>
         </body>
    </html>

    最後訪問index.jsp,將生成以下頁面:

        並且,全部的頁面也會如同index.jsp同樣,被sitemesh的filter使用裝飾模式修改爲如上圖般模樣,卻不用再使用include標籤。

裝飾器     decorator概念
    爲了創建可複用的web應用程序,一個通用的方法是創建一個分層系統,如同下面一個普通的web應用:
  • 前端:JSP和Servlets,或jakarta的velocity 。。。
  • 控制層框架 Controller : (Struts/Webwork)
  • 業務邏輯 Business :主要業務邏輯
  • 持久化框架 :hibernate/jdo

    可糟糕的是前端的頁面邏輯很難被複用,當你在每個頁面中用數之不盡的include來複用公共的header, stylesheet, scripts,footer時,一個問題出現了-重複的代碼,每一個頁面必須用copy來複用頁面結構,而當你須要創意性的改變頁面結構時,災難就愛上了你。

     sitemesh經過filter截取request和response,並給原始的頁面加入必定的裝飾(可能爲header,footer...),而後把結果返回給客戶端,而且被裝飾的原始頁面並不知道sitemesh的裝飾,這也就達到了脫耦的目的。

     聽說即將新出臺的Portlet規範會幫助咱們標準的實現比這些更多更cool的想法,但可憐的我還不懂它究竟是一個什末東東,有興趣的人能夠研究
jetspeed,或JSR (Java Specification Request) 168,但我想sitemesh如此簡單,咱們不妨先用着。

 

讓咱們看看怎樣配置環境
    除了要copy到WEB-INF/lib中的sitemesh.jar外,還有2個文件要創建到WEB-INF/:
  • sitemesh.xml (可選)  
  • decorators.xml

sitemesh.xml 能夠設置2種信息:

Page Parsers :負責讀取stream的數據到一個Page對象中以被SiteMesh解析和操做。(不太經常使用,默認便可)

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>

decorators.xml :定義構成複合視圖的全部頁面構件的描述(主要結構頁面,header,footer...),以下例:

<decorators defaultdir="/decorators">
<decorator name="main" page="main.jsp">
<pattern>*</pattern>
</decorator>
<decorator name="printable" page="printable.jsp" role="customer" webapp="aaa" />
</decorators>
  • defaultdir: 包含裝飾器頁面的目錄
  • page : 頁面文件名
  • name : 別名
  • role : 角色,用於安全
  • webapp : 能夠另外指定此文件存放目錄
  • Patterns : 匹配的路徑,能夠用*,那些被訪問的頁面須要被裝飾。

 

最重要的是寫出裝飾器自己(也就是那些要複用頁面,和結構頁面)。
    其實,重要的工做就是製做裝飾器頁面自己(也就是包含結構和規則的頁面),而後把他們描述到decorators.xml中。

    讓咱們來先看一看最簡單的用法:其實最經常使用也最簡單的用法就是咱們的hello例子,面對如此衆多的技術,我想只要達到功能點到爲止便可,不必去研究太深(除非您有更深的需求)。

<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://www.opensymphony.com/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>

咱們在裝飾器頁面只用了2個標籤:

<decorator:title default="裝飾器頁面..." />       : 把請求的原始頁面的title內容插入到<title></title>中間。

<decorator:body /> : 把請求的原始頁面的body內的所有內容插入到相應位置。

而後咱們在decorator.xml中加入如下描述便可:

<decorator name="main" page="main.jsp">
<pattern>*</pattern>
</decorator>

這樣,請求的全部頁面都會被從新處理,並按照main.jsp的格式從新展示在你面前。

 

讓咱們看看更多的用法。(抄襲sitemesh文檔)
如下列着所有標籤:
Decorator Tags Page Tags
被用於創建裝飾器頁面. 被用於從原始內容頁面訪問裝飾器.
<decorator:head />
<decorator:body />
<decorator:title />
<decorator:getProperty />
<decorator:usePage />
<page:applyDecorator />
<page:param

<decorator:head />

插入原始頁面(被包裝頁面)的head標籤中的內容(不包括head標籤自己)。

<decorator:body />

插入原始頁面(被包裝頁面)的body標籤中的內容。

<decorator:title [ default="..." ] />

插入原始頁面(被包裝頁面)的title標籤中的內容,還能夠添加一個缺省值。

例:

/decorator/main.jsp中 (裝飾器頁面): <title><decorator:title default="卻省title-hello"     /> - 附加標題</title>

/aaa.jsp中 (原始頁面):<title>aaa頁面</title>

訪問/aaa.jsp的結果:<title>aaa頁面 - 附加標題</title>

<decorator:getProperty property="..." [ default="..." ] [ writeEntireProperty="..." ]/>

在標籤處插入原始頁面(被包裝頁面)的原有的標籤的屬性中的內容,還能夠添加一個缺省值。

sitemesh文檔中的例子很好理解:
The decorator: <body bgcolor="white"<decorator:getProperty property="body.onload" writeEntireProperty="true" />>
The undecorated page: <body onload="document.someform.somefield.focus();">
The decorated page: <body bgcolor="white" onload="document.someform.somefield.focus();">

注意,writeEntireProperty="true"會在插入內容前加入一個空格。

<decorator:usePage id="..." />
象jsp頁面中的<jsp:useBean>標籤同樣,可使用被包裝爲一個Page對象的頁面。 (懶的用)

例:可用<decorator:usePage id="page" /> :<%=page.getTitle()%>達到<decorator:title/>的訪問結果。

<page:applyDecorator name="..." [ page="..." title="..." ] >
<page:param name="..."> ... </page:param>
<page:param name="..."> ... </page:param>
</page:applyDecorator>

應用包裝器到指定的頁面上,通常用於被包裝頁面中主動應用包裝器。這個標籤有點很差理解,咱們來看一個例子:

包裝器頁面 /decorators/panel.jsp:<p><decorator:title /></p>     ... <p><decorator:body /></p>
而且在decorators.xml中有<decorator name="panel" page="panel.jsp"/>

一個公共頁面,即將被panel包裝:/public/date.jsp:  
... <%=new java.util.Date()%>     ...<decorator:getProperty property="myEmail" />

被包裝頁面 /page.jsp : 
<title>page的應用</title> 
.....  
     <page:applyDecorator name="panel" page="/_public/date.jsp" >
<page:param name="myEmail"> chen_p@neusoft.com </page:param>
</page:applyDecorator>

最 後會是什末結果呢?除了/page.jsp會被默認的包裝頁面包裝上header,footer外,page.jsp頁面中還內嵌了date.jsp頁 面,而且此date.jsp頁面還會被panel.jsp包裝爲一個title加body的有2段的頁面,第1段是date.jsp的title,第2段 是date.jsp的body內容。

另外,page:applyDecorator中包含的page:param標籤所聲明的屬性值還能夠在包裝頁面中用decorator:getProperty標籤訪問到。

 


可打印的界面裝飾
     前面說過有1種可打印的裝飾器,能夠容許你當用http://localhost/aaa/a.html?printable=true方式訪問時,應用其餘的裝飾器(本身指定),給出原始頁面以供打印(省得把header,footer等的花哨的圖片也搭上)。

讓咱們來看一看怎樣實現他:

1.首先在WEB-INFO/sitemesh.xml中設置:
<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>
這樣就能夠經過?printable=true來使用名爲printable的裝飾器,而不是用原來的裝飾器。

2.在WEB-INFO/decorators.xml中定義相應的printable裝飾器
<decorator name="printable" page="printable.jsp"/>

3.最後編寫printable裝飾器/decorators/printable.jsp

<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<html>
<head>
<title><decorator:title /></title>
<decorator:head />
</head>
<body>

<h1><decorator:title /></h1>
<p align="right"><i>(printable version)</i></p>

<decorator:body />

</body>
</html>

這樣就可讓一個原始頁面經過?printable=true開關來切換不一樣的裝飾器頁面。

 

中文問題
因爲sitemesh內部所使用的缺省字符集爲iso-8859-1,直接使用會產生亂碼,咱們能夠經過如下方法糾正之:
  • 方法1:能夠在您所用的application server的配置文件中找一找,有沒有設置encoding或charset的項目,而後設成gbk或gb2312便可
  • 方法2:這也是咱們一直使用的方法。
    1.在每個jsp頁裏設置: <%@ page contentType="text/html; charset=gbk"%> 來告訴server你所要求的字符集。
    2.在每一個jsp頁的head中定義:<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gbk"> 來告訴瀏覽器你所用的字符集。
總結:使用sitemesh最一般的途徑:

1.配置好環境,

2.在WEB-INFO/decroators.xml中描述你將創建的包裝器。

3.開發在decroators.xml中描述的包裝器,最好存放在/_decorators目錄下

4.ok ,能夠看看辛勤的成果了 :)

 

轉載自:http://www.blogjava.net/shiwenfeng/archive/2009/10/27/299925.html

相關文章
相關標籤/搜索