使用 sitemesh/decorator裝飾器裝飾jsp頁面(原理及詳細配置)
一、SiteMesh介紹
SiteMesh是一個Java WEB項目的網頁布局和修飾框架。使用SiteMesh後就不再需要在每個頁面中都用<jsp:include>標簽引入頁頭、頁尾、導航等其他公用頁面了。
可以將網頁的內容和頁面結構分離,達到頁面結構共享的目的。
頁面裝飾效果耦合在目標頁面中,無需使用include指令顯示包含裝飾效果,目標頁面和裝飾頁面完全分離。
整個web應用可以使用相同的裝飾頁面,風格統一,整體效果更好
SiteMesh通過Filter攔截請求和響應,給原始頁面加入裝飾,再把裝飾後的結果返回給客戶端。
根據頁面URL匹配規則查找合適的裝飾模板頁
提取被訪問頁的內容,放置到裝飾模板中的適當位置。
二、業務場景使用
比如常見的就是crm系統,左邊的樹形菜單就是一致的,變化的右邊主體部分(即被裝飾的頁面)。
三、SiteMesh工作原理
sitemesh應用Decorator模式,用filter截取request和response,把頁面組件head,content,banner、bottom結合為一個完整的視圖。通常我們都是用include標簽在每個jsp頁面中來不斷的包含各種header, stylesheet, scripts and footer。見下圖
當用戶請求home.jsp,並且服務器處理完畢正準備返回數據之時,它被SiteMesh Filter攔截了下來,並且把數據包裝成一個Page對象,具體是Page page = parsePage(request, response, chain)的調用,然後,它會去查詢decorators.xml文件,看看該頁面是否需要裝飾[if (decorator != null && decorator.getPage() != null)]?是,則應用裝飾器[applyDecorator(page, decorator, request, response)],否則,就發送原來的沒經過裝飾的頁面[writeOriginal(response, page);]。
四、sitemesh應用配置
首先我們要到http://www.opensymphony.com/sitemesh/下載我們需要的jar包:sitemesh-2.4.jar
然後分三步走,第一步:web.xml配置;第二步:decorate.xml配置;第三步:裝飾頁面
4.1 web.xml配置
<filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4.2 decorate.xml 配置
在WEB-INF目錄下新建一個decorators.xml文件(/decorator是你的包裝jsp根路徑在這裏main.jsp和panel.jsp都是包裝jsp,a.jsp,b,jsp是被包裝jsp)
<?xml version="1.0" encoding="UTF-8"?><decorators> <excludes> <pattern>/resources/**</pattern> <pattern>/system/login_index.do</pattern> <pattern>/system/login.do</pattern> <pattern>/system/close_window.do</pattern> <pattern>/system/login_force.jsp</pattern> <pattern>/system/info.jsp</pattern> <pattern>/index.jsp</pattern> <pattern>/usermemcached/**</pattern> </excludes> <decorator name="main" page="/system/main.do"> <pattern>/**</pattern> </decorator></decorators>
用decrator指定裝飾模板與URL的對應關系,也可以用excludes配置那些URL不需要模板控制。
4.3 裝飾頁面
<[email protected] contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><[email protected] prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><[email protected] prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %><html> <head> <title> <decorator:title default="default title"/> </title> <decorator:head/> </head> <body /> > <div id="content" class="container" style="width: 100%;"> <c:if test="${not empty actionResult}"> <div class="alert alert-${actionResult.type}"> <button class="close" type="button" data-dismiss="alert">X</button> <spring:message code="${actionResult.message}"></spring:message> </div> </c:if> <!-- 所有被攔截器攔截後,匹配的url頁面都會插入到此 --> <decorator:body></decorator:body> </div> ...... <jsp:include page="/footer.jsp"></jsp:include> </body></html>
參數說明:
<decorator:head />
填充被裝飾頁面的head標簽內容
<decorator:body />
填充被裝飾頁面的body標簽內容
使用 sitemesh/decorator裝飾器裝飾jsp頁面(原理及詳細配置)