天天看點

sitemesh學習筆記(1)

(一)簡介

OS(OpenSymphony)的SiteMesh是一個用來在JSP中實作頁面布局和裝飾(layout and decoration)的架構元件,

能夠幫助網站開發人員較容易實作頁面中動态内容和靜态裝飾外觀的分離。

sitemesh學習筆記(1)

(二)原理

SiteMesh是基于Servlet的filter的,即過濾流。它是通過截取response,并進行裝飾後再傳遞給客戶。其中涉及到兩個名詞: 裝飾頁面(decorator page)和 "被裝飾頁面(Content page)" , 即 SiteMesh通過對Content Page的裝飾,最終得到頁面布局和外觀一緻的頁面,并傳回給客戶 (三)與freemarker的比較:

用sitemesh 的話。你的某個action生成的頁面,隻要是局部就可以。比如生成一個資料
顯示的table,外面的html, header, footer這些都交給sitemesh 去裝飾了;      
如果用freemarker的話。你的某個action生成的頁面需要關注的是整個html, 
隻是你把header, footer, sidebar 這些抽取出來了而已      
(四)使用步驟:
把握幾個要點:jar, 過濾器配置,裝飾器描述檔案(decorators.xml),模闆檔案,标簽引入,
測試頁面
      

1. 下載下傳并導入jar包: sitemesh-2.4.2.jar 2.  在web.xml中配置sitemesh過濾器: <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>         <dispatcher>FORWARD</dispatcher>          <dispatcher>REQUEST</dispatcher>     </filter-mapping>   3. 在web-inf下(預設)建立裝飾器描述檔案:decorators.xml   <?xml version="1.0" encoding="UTF-8"?> <decorators>    <!-- 不需要過濾的請求 -->    <excludes>     <pattern>/static/*</pattern>     <pattern>/remote/*</pattern>    </excludes>    <!-- 定義裝飾器要過濾的頁面 -->    <decorator name="default" page="/decorators/default.jsp">     <pattern>/*</pattern>    </decorator>   </decorators>   如上,定義了一個叫default的裝飾器,對應的模闆檔案在web-root的decorators目錄下,叫做default.jsp、 1)一個decorator 節點定義了一個裝飾器,通過pattern指定要攔截的URL 2)excludes節點指定要排除的URL     4. 建立裝飾器模闆頁面default.jsp,路徑同decorators.xml指定的一緻     <%@ page contentType="text/html;charset=UTF-8"%> <%@ taglib prefix="sitemesh"     uri=" http://www.opensymphony.com/sitemesh/decorator"%> <%@ taglib uri=" http://java.sun.com/jsp/jstl/core"  prefix="c"%> <html> <head> <title>SiteMesh示例首頁面-<sitemesh:title /></title> <sitemesh:head /> </head> <body>     <%@ include file="header.jsp"%>     <div id="content">         <sitemesh:body />     </div>     <%@ include file="footer.jsp"%> </body> </html> sitemesh中常用的三種标簽 <字首:title>: 插入原始頁面(被包裝頁面)的title标簽中的内容,還可以添加一個預設值 <字首:head>: 插入原始頁面(被包裝頁面)的head标簽中的内容(不包括head标簽本身)。 <字首:body>:插入原始頁面(被包裝頁面)的body标簽中的内容     5. 在建立被裝飾頁面供測試   1)webroot中建立一個index頁面 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>sitemesh測試</title> </head> <body> 我的第一個JFinal程式 </body> </html>   2)webroot下的static目錄也建立一個index頁面 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">   <html>     <head>         <title>有人攔截我嗎?</title>     </head>     <body>       有人攔截我嗎?     </body>   </html>     6. 測試 1)通路webroot中的測試頁面index.jsp,sitemesh過濾器會為index.jsp自動插入header和footer部分(當然通路其他頁面也一樣,隻要被攔截) 2)通路static下的index.jsp,發現隻有Index頁面本身的部分,header和footer不會插入,因為在 decorators.xml中被過濾了