<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 19, comments - 53, trackbacks - 0, articles - 283
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    SiteMesh 入門實例

    Posted on 2010-05-11 21:45 Gavin.lee 閱讀(1198) 評論(0)  編輯  收藏 所屬分類: SiteMesh 頁面裝飾組件

    一、SiteMesh設計思想

    用戶發(fā)送request至服務器,服務器根據(jù)此request生成動態(tài)數(shù)據(jù),生成網(wǎng)頁,準備返回給客戶端。就在返回前,SiteMesh進行攔截,對此網(wǎng)頁進行解析,將title、body等部分拆解出來,套上模板后,再返回給客戶端。由于SiteMesh在返回客戶端的最后一步工作,此時的網(wǎng)頁已經(jīng)具備了標準的html網(wǎng)頁格式,因此SiteMesh只需解析標準的html網(wǎng)頁,無需考慮各個Web應用是應用了JSP、ASP,還是Velocity技術(shù),相當靈活。
        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簡單部署到實現(xiàn)

    配置:
    除了要copy到WEB-INF/lib中的sitemesh.jar外,還有2個文件要建立到WEB-INF/:
    sitemesh.xml (可選)  
    decorators.xml
    sitemesh.xml 可以設置2種信息:
    1.Page Parsers :負責讀取stream的數(shù)據(jù)到一個Page對象中以被SiteMesh解析和操作。(不太常用,默認即可)
    2.Decorator Mappers : 不同的裝飾器種類,我發(fā)現(xiàn)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> 


    實現(xiàn):
    這里定義了一個過濾器.所有的請求都交由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下面創(chuàng)建上面定義的模板頁面main.jsp,內(nèi)容如下:

    <%@ 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內(nèi)容插入到<title></title>,比如我們要請求index.jsp頁面的時候。會把index.jsp中的title的內(nèi)容放入到這里

    <decorator:body />


    把請求的原始頁面的body內(nèi)容插入到<body></body>,發(fā)現(xiàn)沒有我們在這句的前面加上了<p>Add head decorator...</p>和<p>Add foot decorator...</p>

    相當于給我們請求的頁面的body內(nèi)容加上了頭部和尾部.實現(xiàn)了模板功能。


    在WEB-INF下創(chuàng)建我們要請求訪問的頁面index.jsp,內(nèi)容如下:

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


    不難發(fā)現(xiàn),我們index.jsp中只有Welcome to the SiteMesh sample... 一句。但是在返回給我們之前套上了main.jsp模板頁。在它的前面和后面分別加上了一句話。通過Sitemesh我們可以很容易實現(xiàn)頁面中動態(tài)內(nèi)容和靜態(tài)裝飾外觀的分離。


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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 色婷婷综合缴情综免费观看| 老汉精品免费AV在线播放| 精品亚洲一区二区三区在线观看| 国产高潮久久免费观看| 中文字幕亚洲第一在线| 女人18特级一级毛片免费视频| 午夜在线免费视频 | 亚洲一级特黄无码片| 99免费视频观看| 久久精品国产亚洲αv忘忧草| 亚洲AV无码乱码精品国产| 一区二区三区无码视频免费福利| 激情五月亚洲色图| 亚洲综合伊人久久综合| 最新中文字幕免费视频| 国产日韩久久免费影院| 亚洲中文字幕无码中文| 久久亚洲综合色一区二区三区| 国产福利在线免费| 中文字幕在线免费播放| 国产精品免费高清在线观看| 亚洲精品中文字幕无码A片老| 亚洲va久久久噜噜噜久久男同| 精品久久免费视频| 在线免费中文字幕| a一级毛片免费高清在线| 亚洲综合色婷婷在线观看| 久久国产精品亚洲综合| 亚洲无码视频在线| 国产美女精品视频免费观看| 国产午夜亚洲精品不卡| 亚洲欧洲高清有无| 久久亚洲综合色一区二区三区| 免费一级毛片免费播放| 国产桃色在线成免费视频| 久久久久免费精品国产| 一级毛片人与动免费观看| 色偷偷亚洲第一综合网| 亚洲首页国产精品丝袜| 亚洲国产高清视频在线观看| 精品亚洲一区二区|