SiteMesh是一個Java WEB項目的網頁布局和修飾框架。使用SiteMesh后就不再需要在每個頁面中都用<jsp:include>標簽引入頁頭、頁尾、導航等其他公用頁面了。
- 可以將網頁的內容和頁面結構分離,達到頁面結構共享的目的。
- 頁面裝飾效果耦合在目標頁面中,無需使用include指令顯示包含裝飾效果,目標頁面和裝飾頁面完全分離。
- 整個web應用可以使用相同的裝飾頁面,風格統一,整體效果更好。
- SiteMesh通過Filter攔截請求和響應,給原始頁面加入裝飾,再把裝飾后的結果返回給客戶端。
- 根據頁面URL查找合適的裝飾模板頁
- 提取被訪問頁的內容,放置到裝飾模板中的適當位置。
用法
1.加入siteMesh Jar包
2.在web.xml中配置siteMesh Filter
WEB-INF/web.xml文件
<web-app ......> ...... <filter> <filter-name>sitemeshFilter</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemeshFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.創建裝飾器配置文件
指定裝飾模板與URL的對應關系,也可以配置那些URL不需要模板控制。
WEB-INF/decorators.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!-- Any urls that are excluded will never be decorated by Sitemesh --> <excludes> <pattern>/Login*</pattern> <patterm>/index.jsp*<pattern> </excludes> <decorators defaultdir="/WEB-INF/views"> <!--defaultdir屬性為模板文件的存放路徑--> <!-- 默認裝飾模板配置, 在需要裝飾的頁面增加<meta name="decorator" content="default"/> --> <decorator name="main" page="layouts/main.jsp" > <pattern>/api/certs/*</pattern> <pattern>/api/provs/*</pattern> <pattern>/api/macs/*</pattern> </decorator> <decorator name="panel" page="layouts/panle.jsp"> </decorator> <!-- 下面可以寫多個 --> </decorators>
decorator標簽屬性
- page 裝飾模板文件
- name 裝飾模板別名
- role 角色
- webapp 單獨指定裝飾文件存放目錄
使用SiteMesh最主要的工作就是創建裝飾模板,然后在decorators.xml配置裝飾模板應用於哪些頁面URL。一般項目可以抽象出主模板,二級頁面模板,三級頁面模板,彈出窗口模板等,但數量往往不會超過8個。
4.創建裝飾模板
WEB-INF/views/layouts/main.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %> <html> <head> <title> <decorator:title default="default title"/> </title> <decorator:head/> </head> <body <decorator:getProperty property=“body.onload" writeEntireProperty=“1"/> > <jsp:include page="/header.jsp"></jsp:include> ...... <decorator:body/> ...... 從meta中獲取變量company的名稱: <decorator:getProperty property=“meta.company”/> ...... <decorator:usePage id=“myPage" /> <%=myPage.getRequest().getAttribute(“username”)%> ...... <jsp:include page="/footer.jsp"></jsp:include> </body> </html>
Sitemesh標簽
<decorator:head />
填充被裝飾頁面的head標簽內容
<decorator:body />
填充被裝飾頁面的body標簽內容
<decorator:title default="default title" />
填充被裝飾頁面的title標簽內容,
<decorator:getProperty property="" default="" writeEntireProperty="{true|false|1|0}"/>
讀取被裝飾頁面中的相關標簽的屬性值,writeEntrieProperty表示只顯示"value",還是顯示"prop=value"
<decorator:usePage id="" />
<%=myPage.getRequest().getAttribute(“username”)%>
將被裝飾頁面構造為一個對象,可以在裝飾頁面的JSP中直接引用。
5.被裝飾頁面
<html lang=“en”> <head> <title>我的sitemesh</title> <meta name="decorator" content="default"/> <meta name=“company” content=“smartdot”/> <meta name=“Author” content=“zhangsan”/> <script> function count(){return 10;} </script> </head> <body onload=“count()”> <p>這是一個被修飾頁面</p> </body> </html>
主動應用裝飾器
在裝飾模板中和被包裝頁面中都可以主動應用裝飾器。使用的標簽為applyDecorator或apply-decorator,可以內嵌param標簽提供參數,裝飾模板中用getProperty標簽可以讀取param提供的參數值。
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%> ...... <body> <page:applyDecorator page="/common/top.jsp" name="panel"> <page:param name="paramName"> ...... </page:param> </page:applyDecorator> ...... </body>
panel裝飾模板中配置:
<div class="...."> <decorator:body/> top.jsp的body <decorator:getProperty property="paramName"/> </div>
applyDecorator屬性
- name 要使用的裝飾模板名,decorators.xml中配置的
- page 要裝飾的頁面
原理
http://my.oschina.net/georgele/blog/49137?p=1
http://my.oschina.net/s2jh/blog/361044
SiteMesh使用攔截器攔截所有請求。
參考文檔
http://m.blog.csdn.net/blog/kpchen_0508/41281749
http://blog.csdn.net/jzh440/article/details/7770013