sitemesh怎么用

這篇文章給大家分享的是有關(guān)sitemesh怎么用的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

昌寧網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,昌寧網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為昌寧上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的昌寧做網(wǎng)站的公司定做!

一,基本概念

1,Sitemesh是一種頁(yè)面裝飾技術(shù) :

 1  :它通過(guò)過(guò)濾器(filter)來(lái)攔截頁(yè)面訪問(wèn)
 2  :根據(jù)被訪問(wèn)頁(yè)面的URL找到合適的裝飾模板
 3  :提取被訪問(wèn)頁(yè)面的內(nèi)容,放到裝飾模板中合適的位置
 4  :最終將裝飾后的頁(yè)面發(fā)送給客戶端。

2,在sitemesh中,頁(yè)面分為兩種:裝飾模板和普通頁(yè)面。
1)裝飾模板,是指用于修飾其它頁(yè)面的頁(yè)面。
2)普通頁(yè)面,一般指各種應(yīng)用頁(yè)面。
3,接下來(lái),我們通過(guò)一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明一下sitemesh修飾網(wǎng)頁(yè)的基本原理。

二,模板修飾網(wǎng)頁(yè)的原理

sitemesh怎么用

sitemesh怎么用

sitemesh怎么用

通過(guò)Sitemesh的注冊(cè)機(jī)制,告訴Sitemesh,當(dāng)訪問(wèn)該路徑時(shí)使用XXX模板(假定使用前面那個(gè)模板)來(lái)修飾被訪問(wèn)頁(yè)面。 

sitemesh怎么用 

當(dāng)用戶在左邊導(dǎo)航欄點(diǎn)擊“戲說(shuō)長(zhǎng)城”( /ShowGreatWall.do)時(shí),右邊的“戲說(shuō)長(zhǎng)城”頁(yè)面將會(huì)被指定的模板修飾

sitemesh怎么用

總結(jié)上面過(guò)程,Sitemesh修飾網(wǎng)頁(yè)的基本原理,可以通過(guò)下面來(lái)說(shuō)明:

sitemesh怎么用

三,Sitemesh的配置與使用

1)WEB-INF/web.xml中加入filter定義與sitemesh的taglib定義

代碼如下:


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


2)創(chuàng)建WEB-INF/decorators.xml,在該文件中配置有哪些模板,以及每個(gè)模板具體修飾哪些URL,另外也可以配置哪些URL不需要模板控制 , decorators.xml的一個(gè)例子如下:

代碼如下:


<excludes>
<pattern>/Login*</pattern>
</excludes>
<decorators defaultdir="/decorators">
   <decorator name="main" page=“DecoratorMainPage.jsp">
       <pattern>/*</pattern>
   </decorator>
   <decorator name=“pop" page=“PopPage.jsp">
       <pattern>/showinfo.jsp*</pattern>
       <pattern>
             /myModule/GreatWallDetailAction.do*
       </pattern>
   </decorator>
</decorators>


3)我們看一個(gè)修飾模板的例子

代碼如下:


<%@page contentType="text/html;?charset=GBK"%>
<%@taglib uri="sitemesh-decorator"?prefix="decorator" %>
<html>
   <head>
        <title> <decorator:title/> </title>
        <decorator:head/>
   </head>
   <body>
       Hello World  <hr/>
       <decorator:body/>
   </body>
</html>


4)我們看一個(gè)被修飾的頁(yè)面的例子:

代碼如下:


<%@ page contentType="text/html;?charset=GBK"%>
<html>
   <head>
       <title>Hello World</title>
   </head>
   <body>
       <p>Decorated page goes here.</p
   </body>
</html>


5)我們看一下裝飾模板中可以使用的Sitemesh標(biāo)簽 

<decorator:head />

 取出被裝飾頁(yè)面的head標(biāo)簽中的內(nèi)容。

<decorator:body />

取出被裝飾頁(yè)面的body標(biāo)簽中的內(nèi)容。

<decorator:title default=""  />

取出被裝飾頁(yè)面的title標(biāo)簽中的內(nèi)容。default為默認(rèn)值

<decorator:getProperty property="" default=""  writeEntireProperty=""/>

取出被裝飾頁(yè)面相關(guān)標(biāo)簽的屬性值。

writeEntireProperty表明,是顯示屬性的值還是顯示“屬性=值”

Html標(biāo)簽的屬性
Body標(biāo)簽的屬性
Meta標(biāo)簽的屬性

注意如果其content值中包含“>或<”會(huì)報(bào)錯(cuò),需轉(zhuǎn)碼,例如&lt;等等

default是默認(rèn)值

<decorator:usePage id="" />

 將被裝飾頁(yè)面構(gòu)造為一個(gè)對(duì)象,可以在裝飾頁(yè)面的JSP中直接引用

6)看一個(gè)在裝飾模板中使用標(biāo)簽的例子

代碼如下:


<html lang=“ <decorator:getProperty property=&lsquo;lang&rsquo;/> ”>
   <head>
       <title> <decorator:title default=“你好” /> </title>
       <decorator:head />
   </head>
   
   <body <decorator:getProperty property=“body.onload" writeEntireProperty=“1"/> >
        從meta中獲取變量company的名稱:
            <decorator:getProperty property=“meta.company”/>
           下面是被修飾頁(yè)面的body中的內(nèi)容:
            <decorator:body />
        <decorator:usePage id=“myPage" />
        <%=myPage.getRequest().getAttribute(“username”)%>
   </body>
</html>


7)看一下相應(yīng)的在被修飾頁(yè)面中的代碼:

代碼如下:


<html lang=“en”>
   <head>
       <title>我的sitemesh</title>
       <meta name=“company” content=“smartdot”/>
       <meta name=“Author” content=“zhangsan”/>
       <script>
            function count(){return 10;}
       </script>
   </head>
   <body onload=“count()”>
        <p>這是一個(gè)被修飾頁(yè)面</p>
   </body>
</html>


四,總結(jié)

1,Sitemesh最為重要的就是做用于修飾的模板,并在decorators.xml中配置這些模板用于修飾哪些頁(yè)面。因此使用Sitemesh的主要過(guò)程就是:做裝飾模板,然后在decorators.xml中配置URL Pattern

2,分析整個(gè)工程,看哪些頁(yè)面需要抽象成模板,例如二級(jí)頁(yè)面、三級(jí)頁(yè)面、彈出窗口等等可能都需要做成相應(yīng)的模板,一般來(lái)說(shuō),一個(gè)大型的OA系統(tǒng),模板不會(huì)超過(guò)8個(gè)。

&mdash; &mdash; &mdash;&mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash; &mdash;

如果某個(gè)特殊的需求請(qǐng)求路徑在過(guò)濾器的范圍內(nèi),但又不想使用模板怎么辦?
你總不能這么不講道理吧!

大家放心吧,SiteMesh早就考慮到這一點(diǎn)了,上面第5步說(shuō)道的decorators.xml這個(gè)時(shí)候就起到作用了!       
下面是我的decorators.xml

代碼如下:


<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/decorators">
   <!-- Any urls that are excluded will never be decorated by Sitemesh -->
   <excludes>
       <pattern>/index.jsp*</pattern>
         <pattern>/login/*</pattern>
   </excludes>
   <decorator name="main" page="main.jsp">
       <pattern>/*</pattern>
   </decorator>
</decorators>


decorators.xml有兩個(gè)主要的結(jié)點(diǎn)
      decorator結(jié)點(diǎn)指定了模板的位置和文件名,通過(guò)pattern來(lái)指定哪些路徑引用哪個(gè)模板
      excludes結(jié)點(diǎn)則指定了哪些路徑的請(qǐng)求不使用任何模板

如上面代碼,/index.jsp和凡是以/login/開(kāi)頭的請(qǐng)求路徑一律不使用模板;

另外還有一點(diǎn)要注意的是:decorators結(jié)點(diǎn)的defaultdir屬性指定了模板文件存放的目錄;

感謝各位的閱讀!關(guān)于“sitemesh怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

網(wǎng)站欄目:sitemesh怎么用
標(biāo)題網(wǎng)址:http://bm7419.com/article10/gegjgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、響應(yīng)式網(wǎng)站網(wǎng)站營(yíng)銷(xiāo)、小程序開(kāi)發(fā)全網(wǎng)營(yíng)銷(xiāo)推廣、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)