spring集成sitemesh3
1、SiteMesh是什么?
SiteMesh是一個(gè)網(wǎng)頁布局和修飾的框架,利用它可以將網(wǎng)頁的內(nèi)容和頁面結(jié)構(gòu)分離,以達(dá)到頁面結(jié)構(gòu)共享的目的。
SiteMesh是基于Servlet的filter,通過截取response,并進(jìn)行裝飾后再交付給客戶。
2、SiteMesh怎么用?
這里使用較新版本SiteMesh3.0(建議使用),和之前版本有點(diǎn)差別。
三步走:
(1)在web.xml中配置 org.sitemesh.config.ConfigurableSiteMeshFilter 過濾器。
(2)創(chuàng)建sitemesh3.xml配置(默認(rèn)路徑:/WEB-INF/sitemesh3.xml),里面可配置哪些路徑的頁面需要被裝飾。
(3)創(chuàng)建SiteMesh模板頁面,用于裝飾頁面。
部分示例代碼:
2.1 在web.xml中配置在web.xml中配置 org.sitemesh.config.ConfigurableSiteMeshFilter 過濾器。
<!--集成sitemesh3 需要配置ConfigurableSiteMeshFilter過濾器--><filter><filter-name>sitemesh</filter-name><filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class></filter><filter-mapping><filter-name>sitemesh</filter-name><url-pattern>/*</url-pattern></filter-mapping>2.2 在/WEB-INF/下面創(chuàng)建sitemesh3.xml并做相關(guān)配置。
<sitemesh><!-- 默認(rèn)裝飾--><mapping decorator="/WEB-INF/view/layouts/default.jsp"/><!--配置特定的裝飾路徑--><!--<mapping path="/index" decorator="/WEB-INF/view/layouts/default.jsp"></mapping>--><!-- 配置不被裝飾的路徑 --><!-- <mapping path="" exclue="true"/>--><mapping path="/test" exclue="true"/><!-- 默認(rèn)情況下,sitemesh 只對(duì) HTTP 響應(yīng)頭中 Content-Type 為 text/html 的類型進(jìn)行攔截和裝飾,我們也可以添加更多的 mime 類型 --><mime-type>text/html</mime-type> </sitemesh>2.3 創(chuàng)建sitemesh模板頁,用于裝飾。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title><sitemesh:write property='title'/></title><sitemesh:write property='head'/> </head> <body> 我是裝飾器,title的內(nèi)容是:<sitemesh:write property='title'/><br/> 我是裝飾器,body的內(nèi)容是:<sitemesh:write property='body'/><br/> </body> </html>到此,三步搞定。
3、參考代碼:
下載Springmvc+Sitemesh完整代碼demo:https://gitee.com/chen_jia_hao/Sitemesh3-demo.git
demo效果:
4、更多--修改sitemesh3.xml默認(rèn)是存放的路徑
前面說到sitemesh3.xml默認(rèn)放在/WEB-INF目錄下面。那么我放到其他地方可不可呢?當(dāng)然可以了。我們一起來看下org.sitemesh.config.ConfigurableSiteMeshFilter過濾器的源代碼,發(fā)現(xiàn)如下:
public static final String CONFIG_FILE_PARAM = "configFile";public static final String CONFIG_FILE_DEFAULT = "/WEB-INF/sitemesh3.xml";configFile 屬性其實(shí)就是用于配置sitemesh3.xml的路徑。下面那一行就是configFile 的默認(rèn)值,所以我們可以在配置過濾器的時(shí)候,還配置一下 configFile 即可。
總結(jié)
以上是生活随笔為你收集整理的spring集成sitemesh3的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AD域账户登录mysql_ASP.NET
- 下一篇: 第一季1:HI3518EV200的体验