無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          SiteMesh 入門實例

          Posted on 2010-05-11 21:45 Gavin.lee 閱讀(1204) 評論(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我們可以很容易實現頁面中動態內容和靜態裝飾外觀的分離。


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 彝良县| 乳源| 田林县| 门头沟区| 威信县| 延吉市| 常宁市| 吴旗县| 富宁县| 永济市| 财经| 军事| 宝坻区| 中超| 紫阳县| 芦山县| 绍兴县| 玉田县| 得荣县| 邹城市| 辽阳市| 九寨沟县| 德州市| 怀安县| 普宁市| 寿阳县| 平顶山市| 西丰县| 淮滨县| 治多县| 获嘉县| 麻江县| 司法| 商河县| 宁明县| 砚山县| 沙田区| 临江市| 新巴尔虎右旗| 盐城市| 济源市|