SiteMesh3配置

SiteMesh3 支持两种主要的配置: XML 和 Java,甚至两者可以共用。

 

XML Java
  • 容易上手
  • 配置更改时可以自动重载
  • 不需要懂得java编程 
  • 更灵活的制定 
  • 不需要多余的配置文件
  • 可以使用JRuby, Groovy, Scala等语言配置

 

 

 

基于xml的配置

配置文件需放置于/WEB-INF/sitemesh3.xml ,如

Xml代码 复制代码  收藏代码
  1. <sitemesh>  
  2.   <mapping path="/*" decorator="/decorator.html"/>  
  3.   <mapping path="/admin/*" decorator="/admin-decorator.html"/>  
  4. </sitemesh>  

 

 

 

基于java的配置

必须编写一个过滤器,继承org.sitemesh.config.ConfigurableSiteMeshFilter 且重载applyCustomConfiguration方法,如

Java代码 复制代码  收藏代码
  1. public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {   
  2.   @Override  
  3.   protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {   
  4.     builder.addDecoratorPath("/*""/decorator.html")   
  5.            .addDecoratorPath("/admin/*""/admin/decorator.html");   
  6.   }   
  7. }  
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
  @Override
  protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
    builder.addDecoratorPath("/*", "/decorator.html")
           .addDecoratorPath("/admin/*", "/admin/decorator.html");
  }
}

 

 

如果xml和java两个配置方法共用,sitemesh3在调用applyCustomConfiguration()方法前会先加载xml的配置。

 

 

配置渲染

  • 给所有路径配置一个默认的渲染
  • 给特殊的路径配置一个渲染
  • 给某个路径配置多个渲染,一个渲染依赖于前面的渲染
  • 排除某个路径

xml

Xml代码 复制代码  收藏代码
  1. <sitemesh>  
  2.   
  3.   <!-- 配置默认的渲染器. 将应用于所有路径. -->  
  4.   <mapping decorator="/default-decorator.html"/>  
  5.   
  6.   <!-- 配置特定路径的渲染器. -->  
  7.   <mapping path="/admin/*" decorator="/another-decorator.html"/>  
  8.   <mapping path="/*.special.jsp" decorator="/special-decorator.html"/>  
  9.   
  10.   <!-- 配置多个渲染器. -->  
  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.   <!-- 不被渲染的路径. -->  
  19.   <mapping path="/javadoc/*" exclue="true"/>  
  20.   <mapping path="/brochures/*" exclue="true"/>  
  21.   
  22. </sitemesh>  

 java

Java代码 复制代码  收藏代码
  1. public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {   
  2.   @Override  
  3.   protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {   
  4.             // 配置默认的渲染器. 将应用于所有路径.   
  5.     builder.addDecoratorPath("/*""/default-decorator.html")   
  6.            // 配置特定路径的渲染器.   
  7.            .addDecoratorPath("/admin/*""/another-decorator.html")   
  8.            .addDecoratorPath("/*.special.jsp""/special-decorator.html")   
  9.            // 配置多个渲染器.   
  10.            .addDecoratorPaths("/articles/*""/decorators/article.html",    
  11.                                              "/decoratos/two-page-layout.html",    
  12.                                              "/decorators/common.html")   
  13.            // 不被渲染的路径.   
  14.            .addExcludedPath("/javadoc/*")   
  15.            .addExcludedPath("/brochures/*");   
  16.   }   
  17. }  
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
  @Override
  protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
            // 配置默认的渲染器. 将应用于所有路径.
    builder.addDecoratorPath("/*", "/default-decorator.html")
           // 配置特定路径的渲染器.
           .addDecoratorPath("/admin/*", "/another-decorator.html")
           .addDecoratorPath("/*.special.jsp", "/special-decorator.html")
           // 配置多个渲染器.
           .addDecoratorPaths("/articles/*", "/decorators/article.html", 
                                             "/decoratos/two-page-layout.html", 
                                             "/decorators/common.html")
           // 不被渲染的路径.
           .addExcludedPath("/javadoc/*")
           .addExcludedPath("/brochures/*");
  }
}

 

对于大多数情况,上面的配置就足够使用了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值