使用 sitemesh/decorator装饰器装饰jsp页面(原理及详细配置)
摘要:首先這個Decorator解釋一下這個單詞:“裝飾器”,我覺得其實(shí)可以這樣理解,他就像我們用到的Frame,他把每個頁面共有的東西提煉了出來,也可能我們也會用各種各樣的include標(biāo)簽,將我們的常用頁面給包括進(jìn)來:比如說頁面的top,bottom這些每個頁面幾乎都有,而且都一樣,如果我們在每個頁面都include,可以發(fā)現(xiàn)我們的程序有很多冗余,重復(fù)。相比之下裝飾器給我們提供了一個較好的選擇,他在你要顯示的頁面根本看不出任何include信息,可以說完全解耦。
一、SiteMesh介紹
SiteMesh是一個Java WEB項(xiàng)目的網(wǎng)頁布局和修飾框架。使用SiteMesh后就不再需要在每個頁面中都用<jsp:include>標(biāo)簽引入頁頭、頁尾、導(dǎo)航等其他公用頁面了。
- 可以將網(wǎng)頁的內(nèi)容和頁面結(jié)構(gòu)分離,達(dá)到頁面結(jié)構(gòu)共享的目的。
- 頁面裝飾效果耦合在目標(biāo)頁面中,無需使用include指令顯示包含裝飾效果,目標(biāo)頁面和裝飾頁面完全分離。
- 整個web應(yīng)用可以使用相同的裝飾頁面,風(fēng)格統(tǒng)一,整體效果更好
- SiteMesh通過Filter攔截請求和響應(yīng),給原始頁面加入裝飾,再把裝飾后的結(jié)果返回給客戶端。
- 根據(jù)頁面URL匹配規(guī)則查找合適的裝飾模板頁
- 提取被訪問頁的內(nèi)容,放置到裝飾模板中的適當(dāng)位置。
?
二、業(yè)務(wù)場景使用
比如常見的就是crm系統(tǒng),左邊的樹形菜單就是一致的,變化的右邊主體部分(即被裝飾的頁面)。
?
?
三、SiteMesh工作原理
??sitemesh應(yīng)用Decorator模式,用filter截取request和response,把頁面組件head,content,banner、bottom結(jié)合為一個完整的視圖。通常我們都是用include標(biāo)簽在每個jsp頁面中來不斷的包含各種header, stylesheet, scripts and footer。見下圖
當(dāng)用戶請求home.jsp,并且服務(wù)器處理完畢正準(zhǔn)備返回數(shù)據(jù)之時,它被SiteMesh Filter攔截了下來,并且把數(shù)據(jù)包裝成一個Page對象,具體是Page page = parsePage(request, response, chain)的調(diào)用,然后,它會去查詢decorators.xml文件,看看該頁面是否需要裝飾[if (decorator != null && decorator.getPage() != null)]?是,則應(yīng)用裝飾器[applyDecorator(page, decorator, request, response)],否則,就發(fā)送原來的沒經(jīng)過裝飾的頁面[writeOriginal(response, page);]。
?
四、sitemesh應(yīng)用配置
首先我們要到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的對應(yīng)關(guān)系,也可以用excludes配置那些URL不需要模板控制。
4.3?裝飾頁面
<%@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 /> ><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>參數(shù)說明:
<decorator:head?/>
? ? 填充被裝飾頁面的head標(biāo)簽內(nèi)容
<decorator:body?/>
? ? 填充被裝飾頁面的body標(biāo)簽內(nèi)容
?
轉(zhuǎn)載于:https://www.cnblogs.com/zishengY/p/7107306.html
總結(jié)
以上是生活随笔為你收集整理的使用 sitemesh/decorator装饰器装饰jsp页面(原理及详细配置)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux系统服务器可能被攻击的几种攻击
- 下一篇: PowerShell_零基础自学课程_8