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

          一、SiteMesh設(shè)計(jì)思想

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

          配置:
          除了要copy到WEB-INF/lib中的sitemesh.jar外,還有2個(gè)文件要建立到WEB-INF/:
          sitemesh.xml (可選)  
          decorators.xml
          sitemesh.xml 可以設(shè)置2種信息:
          1.Page Parsers :負(fù)責(zé)讀取stream的數(shù)據(jù)到一個(gè)Page對(duì)象中以被SiteMesh解析和操作。(不太常用,默認(rèn)即可)
          2.Decorator Mappers : 不同的裝飾器種類,我發(fā)現(xiàn)2種比較有用都列在下面。一種通用的mapper,可以指定裝飾器的配置文件名,另一種可打印的裝飾器,可以允許你當(dāng)用http://localhost/aaa/a.html?printable=true方式訪問(wèn)時(shí)給出原始頁(yè)面以供打印(免得把header,footer等的花哨的圖片也搭上)

          (但一般不用建立它,默認(rèn)設(shè)置足夠了: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> 


          實(shí)現(xiàn):
          這里定義了一個(gè)過(guò)濾器.所有的請(qǐng)求都交由sitemesh來(lái)處理,在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描述各裝飾器頁(yè)面。

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

          各標(biāo)簽常見(jiàn)屬性的含義為:
          defaultdir: 包含裝飾器頁(yè)面的目錄
          page : 頁(yè)面文件名
          name : 別名
          role : 角色,用于安全
          webapp : 可以另外指定此文件存放目錄
          Patterns : 匹配的路徑,可以用*,那些被訪問(wèn)的頁(yè)面需要被裝飾


           在web下面建一個(gè)文件夾取名decorators.在decoratots下面創(chuàng)建上面定義的模板頁(yè)面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="默認(rèn)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>

          說(shuō)明:

          <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>

          此處為decorator標(biāo)簽的聲明。因?yàn)槲覀兿旅嬉褂玫剿?/p>

          <decorator:title />

          把請(qǐng)求的原始頁(yè)面的title內(nèi)容插入到<title></title>,比如我們要請(qǐng)求index.jsp頁(yè)面的時(shí)候。會(huì)把index.jsp中的title的內(nèi)容放入到這里

          <decorator:body />


          把請(qǐng)求的原始頁(yè)面的body內(nèi)容插入到<body></body>,發(fā)現(xiàn)沒(méi)有我們?cè)谶@句的前面加上了<p>Add head decorator...</p>和<p>Add foot decorator...</p>

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


          在WEB-INF下創(chuàng)建我們要請(qǐng)求訪問(wèn)的頁(yè)面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


          頁(yè)面效果如下:

          Add head decorator
          Welcome to the SiteMesh sample 
          Add foot decorator


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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 克山县| 南丰县| 丹凤县| 佛山市| 社旗县| 泊头市| 宁海县| 思南县| 安福县| 石屏县| 平定县| 云阳县| 行唐县| 平度市| 红原县| 晋宁县| 台南市| 临汾市| 山阴县| 双辽市| 读书| 嘉义市| 罗江县| 临湘市| 拜泉县| 平泉县| 镇远县| 蒙山县| 晋城| 雷州市| 铁岭市| 会理县| 襄城县| 绥德县| 依兰县| 清新县| 文山县| 平凉市| 砀山县| 凤凰县| 永善县|