欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

sitemesh教程-頁面裝飾技術(shù)原理及應(yīng)用

  發(fā)布時(shí)間:2013-01-08 11:01:54   作者:佚名   我要評(píng)論
Sitemesh是一種頁面裝飾技術(shù):它通過過濾器(filter)來攔截頁面訪問,據(jù)被訪問頁面的URL找到合適的裝飾模板等等,感興趣的朋友可以了解下哦

一,基本概念

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

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

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

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







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

 

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



總結(jié)上面過程,Sitemesh修飾網(wǎng)頁的基本原理,可以通過下面來說明:



三,Sitemesh的配置與使用

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

復(fù)制代碼
代碼如下:

<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è)例子如下:

復(fù)制代碼
代碼如下:

<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è)修飾模板的例子

復(fù)制代碼
代碼如下:

<%@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è)被修飾的頁面的例子:

復(fù)制代碼
代碼如下:

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

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

<decorator:body />

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

<decorator:title default=""  />

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

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

取出被裝飾頁面相關(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="" />

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

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

復(fù)制代碼
代碼如下:

<html lang=“ <decorator:getProperty property=‘lang’/> ”>
<head>
<title> <decorator:title default=“你好” /> </title>
<decorator:head />
</head>

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

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

復(fù)制代碼
代碼如下:

<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è)被修飾頁面</p>
</body>
</html>

四,總結(jié)

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

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

— — —— — — — — — — — — — — — — — — — — — — — — —

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

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

復(fù)制代碼
代碼如下:

<?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)指定了模板的位置和文件名,通過pattern來指定哪些路徑引用哪個(gè)模板
      excludes結(jié)點(diǎn)則指定了哪些路徑的請(qǐng)求不使用任何模板

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

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

相關(guān)文章

最新評(píng)論