sitemesh3.0的配置以及在靜態html中的使用


 

sitemesh3配置.md

引言

白天的時候一直想使用sitemesh來整合spring mvc+velocity+mybatis架構,但是在度娘搜了很久都沒搜到想要的資料,同時sitemesh官網又被偉大的GFW給屏蔽了(反正VPN之前我是經常訪問不了),因此搞了好幾個小時都沒搞明白究竟如何在velocity中使用sitemesh來裝飾我的頁面。於是乎,晚上借着vpn的由頭總算在google上很快搜到了想要的資料。Wiki中的資料很全,不過是英文版的,因此本文中的敘述會將原文檔的一部分做一些簡略的翻譯,若理解有誤還請大家及時指出,藉此文章共享的同時也可以更好的彼此學習。

SiteMesh 3 概述

  1. SiteMesh 是類似於tiles框架的一種,但個人覺得要比tiles的配置簡單很多。同tiles一樣,Sitemesh可以將不同的頁面進行組合,例如:很多情況下一個網站的 head部分以及footer部分對於大多數頁面來說都是完全一樣的,如果每一個頁面都簡單的使用復制粘貼,那么當某一個頁面的導航欄變動時,你將會發現:“天啊!我不過只是想改某個導航欄的一個字而已,但我竟然需要一個一個改掉所有的頁面!”,如果你只有五個導航欄,那么或許你可以較快速的改掉五個頁面的相應部分,但是如果你有10個導航欄……。
  2. ok,或許你會說:我可以使用iframe框架,這樣就可以將 head和footer分別裝載到兩個iframe結構中……er....相信很多人會認為這是一個很好的辦法,好吧,不可否認,如果是后台管理系統,這無疑是一個很好的辦法。但對於前台展示頁面則顯然不適合采用frame的方式來進行布局。而tiles在后續的使用中我發現太過繁瑣,於是找到了sitemesh這種相對輕盈且配置簡便的框架來替代tiles框架,在我看來他們最大的不同是tiles框架需要對每個被裝飾的頁面進行部分相應的配置,而對於sitemesh架構來說,被裝飾的頁面本身並不知道自己是否已經被sitemesh的裝飾器驅動過了,對於用戶所訪問的請求頁面而言,裝飾器的頁面組合完全是透明的。也或許會有人想到include ,jsp include 等類似標簽,但是對於spring mvc + velocity + mybatis的架構來說,jsp頁面當真是不需要的,因此也就喪失了這些標簽的能力。當然,velocity本身就具有類似的組合頁面的功能,但是他還是有部分局限性的,於是乎便出現了tiles(struts1出現時便有了它),繼tiles之后則有sitemesh。
    SiteMesh能做什么,下面附原版的一幅插圖:

SiteMesh3中相比於之前的2.x版本有什么新的功能?

版本3在版本2的基礎上進行了代碼的重構,使之效率更高且更易於使用和進行功能的擴展。

1.全新的內容處理器架構

內部的Sitemesh的內容處理機制發生了根本性的變化,提高了吞吐量,降低了近一半的內存使用率。

2.裝飾器鏈

在2.x版本中,sitemesh僅能進行一次裝飾,無法使用裝飾嵌套,這也是我一直覺得比較遺憾的地方,即你的要顯示的內容頁面僅能被預先定義的裝飾器頁面裝飾一次,而被裝飾過的頁面是無法再次被裝飾的。
3.0版本中對我來說最喜歡的更新就是裝飾器鏈了,它可以進行裝飾的層層遞進,你可以將一個頁面通過多個裝飾器進行裝飾,代碼配置如下:

<mapping>
    <path>/*</path>
    <decorator>/header.html</decorator>
    <decorator>/body.html</decorator>
    <decorator>/footer.html</decorator>
</mapping>

 

上面的意思就是這個Filter將會攔截所有請求,之后先將該請求的頁面放入裝飾頁面 header.html中,然后將裝飾后的頁面一起放入body.html,最后再穿過footer.html頁面,當然,前一個裝飾后的頁面是不知道后一個頁面的裝飾的。具體使用教程將在后面的部分介紹。

3.不再依賴於模板框架

3.0版本sitemesh已經不再依賴於jsp,velocity類型框架了,他可以簡簡單單的裝飾任何語言的頁面,如php、asp等等等等,甚至是靜態頁html都不在話下。而2.x版本中,只能通過velocity、jsp、freemarker等進行驅動

4.更加簡便的配置過程

熟悉2.x版本的朋友們都知道,2.x版本的配置相對比較繁瑣,配置sitemesh.xml時需要顯示聲明一些屬性設置過程,而3.x版本則僅需寥寥幾行甚至一行便可輕松配置出裝飾頁面。配置文件區別如圖:

2.x版本配置(⊙﹏⊙b,好多復雜的東東……):

3.0版本配置(簡單的令人發指!!)

5.代碼的高可擴展性

采用了更加清晰的代碼結構和風格,更加簡便的API操作,以及令人發指的高擴展性使得制作自己的app成為可能。

6.將使用協議更新為Apache Software License v2.0

這種協議使得sitemesh擁有更廣闊的使用范圍和空間,可以授權給許多組織和機構。

SiteMesh 3 開始使用

1.將sitemesh的Filter寫入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>

 

2.在預先設置的裝飾頁面中使用 (頁面為body.html)

<html>
  <head>
    <title><sitemesh:write property='title'/></title>
    <sitemesh:write property='head'/>
  </head>
  <body>
    <sitemesh:write property='body'/>
  </body>
</html>

 

此處需要注意一下,2.x版本中,sitemesh取得用戶請求的內容頁面時 如果是JSP 是通過sitemesh標簽實現的,velocity中則是通過${body} 這一類的規則來實現的,而在sitemesh3.0版本中,則改用了自定義的html標簽,見上述代碼中 實際類似於2.x版本 velocity中的${head}語句

3.配置sitemesh的xml文件(/WEB-INF/sitemesh.xml)

<sitemesh>
  <mapping path="/*" decorator="/body.html"/>
</sitemesh>

 

Sitemesh3的文件配置方式與Java代碼兩種方式的寫法

Sitemesh3提供了更簡便的代碼操作,可以通過代碼的方式輕易的操控裝飾器的各類配置,具體實現如下:

基本配置

The configuration file should live in /WEB-INF/sitemesh3.xml in your web-application.
Example

<sitemesh>
  <mapping path="/*" decorator="/decorator.html"/>
  <mapping path="/admin/*" decorator="/admin-decorator.html"/>
</sitemesh>

 

Java based configuration
To use the Java based configuration, subclass org.sitemesh.config.ConfigurableSiteMeshFilter and overload theapplyCustomConfiguration(SiteMeshFilterBuilder builder) method. You shall be passed an object that you can use to configure SiteMesh. You then deploy this filter in to your web-application.
Example

public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
  @Override
  protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) { 
    builder.addDecoratorPath("/*", "/decorator.html")
           .addDecoratorPath("/admin/*", "/admin/decorator.html");
  }
}

 

配置裝飾器鏈

XML方式

 1 <sitemesh>
 2   <!-- Map default decorator. This shall be applied to all paths if no other paths match. -->
 3   <mapping decorator="/default-decorator.html"/>
 4 
 5   <!-- Map decorators to path patterns. -->
 6   <mapping path="/admin/*" decorator="/another-decorator.html"/>
 7   <mapping path="/*.special.jsp" decorator="/special-decorator.html"/>
 8 
 9   <!-- Alternative convention. This is more verbose but allows multiple decorators
10        to be applied to a single path. -->
11   <mapping>
12     <path>/articles/*</path>
13     <decorator>/decorators/article.html</decorator>
14     <decorator>/decorators/two-page-layout.html</decorator>
15     <decorator>/decorators/common.html</decorator>
16   </mapping>
17 
18   <!-- Exclude path from decoration. -->
19   <mapping path="/javadoc/*" exclue="true"/>
20   <mapping path="/brochures/*" exclue="true"/>
21 
22 </sitemesh>

 

Java方式

 1 public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
 2 
 3   @Override
 4   protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
 5            // Map default decorator. This shall be applied to all paths if no other paths match.
 6     builder.addDecoratorPath("/*", "/default-decorator.html") 
 7            // Map decorators to path patterns. 
 8            .addDecoratorPath("/admin/*", "/another-decorator.html")
 9            .addDecoratorPath("/*.special.jsp", "/special-decorator.html")
10            // Map multiple decorators to the a single path.
11            .addDecoratorPaths("/articles/*", "/decorators/article.html",
12                                              "/decoratos/two-page-layout.html", 
13                                              "/decorators/common.html")
14            // Exclude path from decoration.
15            .addExcludedPath("/javadoc/*")
16            .addExcludedPath("/brochures/*");
17   }
18 
19 }

 

定義自己的裝飾器規則

Deploying Tag Rule Bundles
An advanced feature of SiteMesh is the ability to define custom rules that manipulate tags on a page. These are classes that implementorg.sitemesh.content.tagrules.TagRuleBundle.
XML方式

1 <sitemesh>
2   <content-processor>
3     <tag-rule-bundle class="com.something.CssCompressingBundle" />
4     <tag-rule-bundle class="com.something.LinkRewritingBundle"/>
5   </content-processor>
6   ...
7 </sitemesh>

 

Java方式

 1 package com.feifei
 2 public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
 3 
 4 
 5   @Override
 6   protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
 7     builder.addTagRuleBundles(new CssCompressingBundle(), new LinkRewritingBundle());
 8   }
 9 
10 
11 }

 

需要注意的是,如果采用java的方式,則在web.xml中的配置應改為

 1 <web-app>
 2   ...
 3 
 4   <filter>
 5     <filter-name>sitemesh</filter-name>
 6     <filter-class>com.feifei.
 7   </filter>
 8 
 9   <filter-mapping>
10     <filter-name>sitemesh</filter-name>
11     <url-pattern>/*</url-pattern>
12   </filter-mapping>
13 
14 </web-app>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM