Java SiteMesh新手學(xué)習(xí)教程代碼案例
官網(wǎng):http://wiki.sitemesh.org/wiki/display/sitemesh/Home
也可以下載官方的示例Demo參考和學(xué)習(xí),這里我只做一個(gè)簡單示例,演示最基本的使用
首先就是加Jar包,我用的是sitemesh-2.4.2.jar,然后在web.xml中增加過濾器:
<?xml version='1.0' encoding='UTF-8'?> <web-app version='2.5' xmlns='http://java.sun.com/xml/ns/javaee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> </filter-mapping> </web-app>
增加SiteMesh配置文件decorators.xml,該文件放在WEB-INF下:
<?xml version='1.0' encoding='UTF-8'?><decorators defaultdir='/layouts/'> <!-- 不需要過濾的請求 --> <excludes> <pattern>/static/*</pattern> <pattern>/remote/*</pattern> </excludes> <!-- 定義裝飾器要過濾的頁面 --> <decorator name='default' page='default.jsp'> <pattern>/*</pattern> </decorator></decorators>
在根目錄下新建文件夾layouts,然后新建三個(gè)JSP,一個(gè)是默認(rèn),一個(gè)輸出頭,一個(gè)輸出尾,默認(rèn)頁面引用其他兩個(gè)。默認(rèn)頁面default.jsp:
<%@ page contentType='text/html;charset=UTF-8'%><%@ taglib prefix='sitemesh' uri='http://www.opensymphony.com/sitemesh/decorator' %> <%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%><html><head><title>SiteMesh示例-<sitemesh:title/></title><sitemesh:head/></head><body> <%@ include file='/layouts/header.jsp'%> <div id='content'> <sitemesh:body/> </div> <%@ include file='/layouts/footer.jsp'%></body></html>
簡單說明:
引入了SiteMesh標(biāo)簽。 <sitemesh:title/> 會(huì)自動(dòng)替換為被過濾頁面的title。 <sitemesh:head/> 會(huì)把被過濾頁面head里面的東西(除了title)放在這個(gè)地方。 <sitemesh:body/> 被過濾的頁面body里面的內(nèi)容放在這里。頭部引入js和css,都可以在其他重用。
頭頁面header.jsp:
<%@ page language='java' import='java.util.*' pageEncoding='UTF-8'%>
菜單信息
尾頁面footer.jsp:
<%@ page language='java' import='java.util.*' pageEncoding='UTF-8'%> 版權(quán)信息
在根下新建一個(gè)文件夾static,用于實(shí)驗(yàn)是否攔截,在該文件夾下新建JSP:
<%@ page language='java' import='java.util.*' pageEncoding='UTF-8'%> <% String path = request.getContextPath(); String basePath = request.getScheme()+'://'+request.getServerName()+':'+request.getServerPort()+path+'/'; %> <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'> <html> <head> <base href='http://m.4tl426be.cn/bcjs/<%=basePath%>' rel='external nofollow' > <title>有人攔截我嗎?</title> </head> <body> 有人攔截我嗎? </body> </html>
訪問:http://127.0.0.1:8080/sitemesh/index.jsp這個(gè)會(huì)攔截
訪問:http://127.0.0.1:8080/sitemesh/static/index.jsp則不會(huì)攔截處理
根據(jù)頁面看實(shí)際效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)2. python 寫函數(shù)在一定條件下需要調(diào)用自身時(shí)的寫法說明3. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法4. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))5. JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表6. 利用CSS制作3D動(dòng)畫7. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解8. 存儲于xml中需要的HTML轉(zhuǎn)義代碼9. SpringBoot+TestNG單元測試的實(shí)現(xiàn)10. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程
