Sitemesh 3 的使用及配置

1、Sitemesh 3 簡介

Sitemesh 是一個(gè)網(wǎng)頁布局和修飾的框架,基于 Servlet 中的 Filter。

官網(wǎng):http://wiki.sitemesh.org/wiki/display/sitemesh/Home 。

2、Sitemesh 3 下載

這里使用Maven下載,pom.xml

<dependency>
   <groupId>org.sitemesh</groupId>
   <artifactId>sitemesh</artifactId>
   <version>3.0.0</version>
</dependency>
3、配置 Sitemesh 3 過濾器

在web.xml中添加一下過濾器

<web-app>

  ...

  <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>
  
</web-app>
4、準(zhǔn)備一下兩個(gè)頁面

webapp/demo.jsp 和 webapp/WEB-INF/views/decorators/decorator.jsp

demo.jsp - "被裝飾的頁面",實(shí)際要呈現(xiàn)的內(nèi)容頁

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>中文標(biāo)題·</title>
</head>
<body>
    中文內(nèi)容
</body>
</html>

decorator.jsp - "裝飾頁面",所謂的"模板頁面"

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
    <sitemesh:write property="title" /> - ltcms
</title>
<sitemesh:write property="head" />
</head>
<body>
    <header>header</header>
    <hr />
    demo.html的title將會(huì)填充在這里;
    <sitemesh:write property="title" />
    <br /> demo.html的body將會(huì)填充在這里;
    <sitemesh:write property="body" />
    <br />
    <hr />
    <footer>footer</footer>
</body>
</html>
5、 添加 /WEB-INF/sitemesh3.xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
    <!-- 指明滿足“/*”的頁面,將被“/WEB-INF/views/decorators/decorator.html”所裝飾 -->
    <mapping path="/*" decorator="/WEB-INF/view/decorators/decorator.jsp" />

    <!-- 指明滿足“/exclude.jsp*”的頁面,將被排除,不被裝飾 -->
    <mapping path="/exclude.jsp*" exclue="true" />
</sitemesh>
6、運(yùn)行效果
運(yùn)行結(jié)果
7、sitemesh3.xml詳細(xì)配置
<sitemesh>
    <!--默認(rèn)情況下,
        sitemesh 只對(duì) HTTP 響應(yīng)頭中 Content-Type 為 text/html 的類型進(jìn)行攔截和裝飾,
        我們可以添加更多的 mime 類型-->
  <mime-type>text/html</mime-type>
  <mime-type>application/vnd.wap.xhtml+xml</mime-type>
  <mime-type>application/xhtml+xml</mime-type>
  ...
  
  <!-- 默認(rèn)裝飾器,當(dāng)下面的路徑都不匹配時(shí),啟用該裝飾器進(jìn)行裝飾 -->
  <mapping decorator="/default-decorator.html"/>
  
  <!-- 對(duì)不同的路徑,啟用不同的裝飾器 -->
  <mapping path="/admin/*" decorator="/another-decorator.html"/>
  <mapping path="/*.special.jsp" decorator="/special-decorator.html"/>

  <!-- 對(duì)同一路徑,啟用多個(gè)裝飾器 -->
  <mapping>
    <path>/articles/*</path>
    <decorator>/decorators/article.html</decorator>
    <decorator>/decorators/two-page-layout.html</decorator>
    <decorator>/decorators/common.html</decorator>
  </mapping>

  <!-- 排除,不進(jìn)行裝飾的路徑 -->
  <mapping path="/javadoc/*" exclue="true"/>
  <mapping path="/brochures/*" exclue="true"/>
  
  <!-- 自定義 tag 規(guī)則 -->
  <content-processor>
    <tag-rule-bundle class="com.something.CssCompressingBundle" />
    <tag-rule-bundle class="com.something.LinkRewritingBundle"/>
  </content-processor>
  ...

</sitemesh>

本文并非原創(chuàng),原文地址:https://www.cnblogs.com/luotaoyeah/p/3776879.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容