页面修饰框架SiteMesh的简单使用
生活随笔
收集整理的這篇文章主要介紹了
页面修饰框架SiteMesh的简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
SiteMesh框架是由OpenSymphony組織來開發的,下載地址:http://www.opesymphony.com/sitemesh/download.action
?使用步驟:
將下載的sitemesh-2.4.jar文件復制到Web應用的lib下
為了能是SiteMesh框架可以處理所有的用戶請求,修改web.xml文件,配一個Filter,class為com.opensymphony.module.sitemesh.filter.PageFilter,?? url-mapping 為/*
要在Jsp頁面中使用SiteMesh的標簽庫。還要導入SiteMesh的標簽庫,SiteMesh提供了兩個標簽庫,一個是page還有一個是decorator
?
本文為大家展示一個簡單的SiteMesh例子。
???? 首先創建一個web工程.名字就叫做SitemeshSample.將sitemesh-2.3.jar放到lib目錄下。
在web.xml中加入如下片段:
??? <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>
??? 這里定義了一個過濾器.所有的請求都交由sitemesh來處理
?
???? 在WEB-INF下創建一個decorators.xml文件,內容如下:
??? <?xml version="1.0" encoding="UTF-8"?>
??? <decorators defaultdir="/decorators">
??? ??? <decorator name="main" page="main.jsp">
?????? ??? <pattern>/*</pattern>
??? ??? </decorator>
??? </decorators>
??? 這是定義了模板頁,也就是所有頁面在返回給客戶端之前,先在這里加上裝飾,套上模板。
defaultdir="/decorators"說明了模板頁的路徑。<decorator name="main" page="main.jsp">模板頁的名稱。??? <pattern>/*</pattern>表示對所有的response進行處理
?
??? 在web下面建一個文件夾取名decorators.在decoratots下面創建上面定義的模板頁面main.jsp,內容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
??? <head>
?????? <title><decorator:title />
?????? </title>
??? <body>
?????? <p>Add head decorator...</p>
?????? <decorator:body />
?????? <p>Add foot decorator...</p>
??? </body>
</html>
說明:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
此處為decorator標簽的聲明。因為我們下面要使用到它
<decorator:title />
把請求的原始頁面的title內容插入到<title></title>,比如我們要請求index.jsp頁面的時候。會把index.jsp中的title的內容放入到這里
<decorator:body />
把請求的原始頁面的body內容插入到<body></body>,發現沒有我們在這句的前面加上了<p>Add head decorator...</p>和<p>Add foot decorator...</p>
相當于給我們請求的頁面的body內容加上了頭部和尾部.實現了模板功能。
?
在WEB-INF下創建我們要請求訪問的頁面index.jsp,內容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
??? <head>
?????? <title>SiteMesh Sample Site</title>
??? </head>
?
??? <body>
?????? Welcome to the SiteMesh sample...
??? </body>
</html>
把web工程部署到tomcat容器中。
輸入http://localhost:8080/SitemeshSample/index.jsp
?
頁面效果如下:
Add head decorator...
Welcome to the SiteMesh sample...
Add foot decorator...
不難發現,我們index.jsp中只有Welcome to the SiteMesh sample... 一句。但是在返回給我們之前套上了main.jsp模板頁。在它的前面和后面分別加上了一句話。通過Sitemesh我們可以很容易實現頁面中動態內容和靜態裝飾外觀的分離。
轉載于:https://my.oschina.net/china008/blog/335396
總結
以上是生活随笔為你收集整理的页面修饰框架SiteMesh的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络基础专业找工作,2021计算机
- 下一篇: 小程序 ajax 加载,小程序实战-小程