com.opensymphony.module.sitemesh.filter.pagefilter作用

1.sitemesh簡介

2.與strust tiles比較

3.基本原理

4.簡單例子

5.總結

6.參考資料

1.sitemesh簡介

     sitemesh是由一個基於web頁面佈局、裝飾以及與現存web應用整合的框架。它能幫助我們在由大量頁面構成的項目中創建一致的頁面佈局和外 觀,如一致的導航條,一致的banner,一致的版權,等等。它不僅僅能處理動態的內容,如jsp,php,asp等產生的內容,它也能處理靜態的內容, 如htm的內容,使得它的內容也符合你的頁面結構的要求。甚至於它能將html文件象include那樣將該文件作爲一個面板的形式嵌入到別的文件中去。 所有的這些,都是gof的decorator模式的最生動的實現。儘管它是由java語言來實現的,但它能與其他web應用很好地集成。

2.與strust tiles比較

     從使用角度來看,tiles似乎是sitemesh標籤<page:applydecorator>的一個翻版。其實sitemesh 最強的一個特性是sitemesh將decorator模式用在過濾器上。任何需要被裝飾的頁面都不知道它要被誰裝飾,所以它就可以用來裝璜來自php、 asp、cgi等產生的頁面了。你可以定義若干個裝飾器,根據參數動態地選擇裝飾器,產生動態的外觀以滿足你的需求。它也有一套功能強大的屬性體系,它能 幫助你構建功能強大而靈活的裝飾器。相比較而言,在這方面tiles就遜色許多。

3.基本原理

    一個請求到服務器後,如果該請求需要sitemesh裝飾,服務器先解釋被請求的資源,然後根據配置文件獲得用於該請求的裝飾器,最後用裝飾器裝飾被請求資源,將結果一同返回給客戶端瀏覽器。

下邊是由jsp和cgi產生的兩個頁面,它們通過sitemesh裝飾後,形成了一致的用戶界面。

4.簡單例子

   環境說明:

   windows2k+、tomcat4.0+

1.安裝:

    將sitemesh-2.1.jar複製到lib目錄下;修改web.xml文件,也就是在web.xml中加入粗傾體標記的部分;

<?xml version="1.0" encoding="gb2312"?>

<! doctype web-app public "-//sun microsystems, inc.//dtd web application  2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<filter>

        <filter-name>sitemesh</filter-name>

        <filter-lass>com.opensymphony.module.sitemesh.filter.pagefilter</filter-class></filter>

<filter-mapping>

        <filter-name>sitemesh</filter-name>

        <url-pattern>/*</url-pattern>

</filter-mapping>


</web-app>

2.建立一個目錄,名字叫decorators,當然你也可以取別的,用於放裝飾器(也就是一個使用了sitemesh標籤的jsp文件)用;

3.定義一個你的模板並把它放到decorators目錄下,我的文體名稱叫:mymain.jsp文件內容如下:

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

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


<html>

<head>

<meta http-equiv="content-language" content="zh-cn">

<meta http-equiv="content-type" content="text/html; charset=gb2312">

<meta name="generator" content="microsoft frontpage 4.0">

<meta name="progid" content="frontpage.editor.document">

<title>ap - <decorator:title default="welcome to architecture pub!" />

</title>

<decorator:head />

</head>

< body><table border="0" cellpadding="2" width="100%" height= "151">  <tr>    <td width="20%" height="74">      <p  align="center"><font color="#000080" size="5">test sitemesh< /font></td>    <td width="73%" height="74"></td>   </tr>  <tr><td width="20%" height="28" bgcolor="#f3f3f3"  valign="top" >

<page:applydecorator page="/menu.jsp" name="panel" />

</td>    <td width="73%" height="28" bgcolor="#bed6fa">

<decorator:body />

< /td>  </tr>  <tr>    <td width="100%" height="31"  colspan="2" bgcolor="#f1f0ed">      <p align="center">powered  by gagaghost</td>  </tr></table></body>< /html>

說明:

<decorator:title default="welcome to test sitemesh!" />:讀取被裝飾頁面的標題,並給出了默認標題。

<decorator:head />:讀取被裝飾頁面的<head>中的內容;

<page:applydecorator page="/menu.jsp" name="panel" />:把menu.jsp文件用panel指定的裝飾器裝飾,並把結果插入到該位置;

<decorator:body />:讀取被裝飾頁面的<body>中的內容;

當然還有很多的標籤,這裏不再詳細描述。

4.在docorators.xml文件裏配置剛寫好的模板mymain.jsp,內容如下:

<?xml version="1.0" encoding="iso-8859-1"?>

<decorators defaultdir="/decorators">

    <decorator name="main" page="mymain.jsp">

        <pattern>/*</pattern>

</decorator>

    <decorator name="panel" page="panel.jsp"/>

    <decorator name="printable" page="printable.jsp"/>

</decorators>

這裏我是把對所有的請求都用mymail.jsp來裝飾。

5.好了,run一下你的“小板凳”!你會看到如下結果:

5.總結

     使用sitemesh給我們帶來的是不僅僅是頁面結構問題,它的出現讓我們有更多的時間去關注底層業務邏輯,而不是整個頁面的風格和結構。它讓我們 擺脫了大量用include方式複用頁面尷尬局面,它也提供了很大的靈活性以及給我們提供了整合異構web系統頁面的一種方案。我們期望它在未來的版本中 會有更多的特性出現。

6.參考資料

http://www.opensymphony.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章