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>

 

posted @ 2014-07-05 11:18  Andrew Lee  阅读(4069)  评论(1编辑  收藏  举报