Java SiteMesh新手學習教程代碼案例
官網:http://wiki.sitemesh.org/wiki/display/sitemesh/Home
也可以下載官方的示例Demo參考和學習,這里我只做一個簡單示例,演示最基本的使用
首先就是加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,然后新建三個JSP,一個是默認,一個輸出頭,一個輸出尾,默認頁面引用其他兩個。默認頁面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標簽。 <sitemesh:title/> 會自動替換為被過濾頁面的title。 <sitemesh:head/> 會把被過濾頁面head里面的東西(除了title)放在這個地方。 <sitemesh:body/> 被過濾的頁面body里面的內容放在這里。頭部引入js和css,都可以在其他重用。
頭頁面header.jsp:
<%@ page language='java' import='java.util.*' pageEncoding='UTF-8'%>
菜單信息
尾頁面footer.jsp:
<%@ page language='java' import='java.util.*' pageEncoding='UTF-8'%> 版權信息
在根下新建一個文件夾static,用于實驗是否攔截,在該文件夾下新建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://www.aoyou183.cn/bcjs/<%=basePath%>' rel='external nofollow' > <title>有人攔截我嗎?</title> </head> <body> 有人攔截我嗎? </body> </html>
訪問:http://127.0.0.1:8080/sitemesh/index.jsp這個會攔截
訪問:http://127.0.0.1:8080/sitemesh/static/index.jsp則不會攔截處理
根據頁面看實際效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: