Sitemesh 3 配置和使用(最新)

Sitemesh 3 配置和使用(最新)

一 Sitemesh简介

  • Sitemesh是一个页面装饰器,可以快速的创建有统一外观Web应用 -- 导航 加 布局 的统一方案~
  • Sitemesh可以拦截任何动态或则静态的HTML页面的请求,Sitemesh处理后把一个或多个装饰器组装成最后结果返回
  • Sitemesh可以把一个大页面分成很多小的页面来布局

Sitemesh官网简介图片简单明了,一目了然 .. welcome Page和search Page包含两部分 Meta-Data 和 Body-Content , 通过装饰器后被装饰返回一个最终的页面 final pages.

官网 : http://wiki.sitemesh.org/wiki/display/sitemesh3/Home


二 Maven中使用Sitemesh 3

在Maven工程的Pom中添加依赖 ~


org.sitemesh
sitemesh
3.0.0


三 在web.xml中配置Sitemesh 3 拦截器

...


sitemesh
org.sitemesh.config.ConfigurableSiteMeshFilter


sitemesh
/*


四 sitemesh 3 的配置

sitemesh 3 有两种配置方式 XML和Java

1 XML配置详解

添加一个 ~ /WEB-INF/sitemesh3.xml









/articles/*
/decorators/article.html
/decorators/two-page-layout.html
/decorators/common.html





text/html
application/vnd.wap.xhtml+xml
application/xhtml+xml





4 Java的配置方式

package com.erma.common;

import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;

/**
* Created by Erma on 2017/4/13.
*/
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {

@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
// 默认的装饰器
// Map default decorator. This shall be applied to all paths if no other paths match.
builder.addDecoratorPath("/", "/default-decorator.html")
// 配合 自己 的 路径对应的装饰器
// Map decorators to path patterns.
.addDecoratorPath("/admin/
", "/another-decorator.html")
.addDecoratorPath("/.special.jsp", "/special-decorator.html")
//一个路径多个装饰器
// Map multiple decorators to the a single path.
.addDecoratorPaths("/articles/
", "/decorators/article.html",
"/decoratos/two-page-layout.html",
"/decorators/common.html")
// 配置不被装饰的路径
// Exclude path from decoration.
.addExcludedPath("/javadoc/")
.addExcludedPath("/brochures/
");

// 配置自己的MineType
builder.setMimeTypes("text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml");
// 配置自己的tag
builder.addTagRuleBundles(new CssCompressingBundle(), new LinkRewritingBundle());
}
}


五 Sitemesh 3 的使用

下面是使用 intellij + Maven + Sitemsehxml的配置方式的一个使用

1 配置Maven的pom 和web.xml按照上面的方式

2 配置sitemseh.xml





3 配置自定义的tag

package com.erma.common;

import org.sitemesh.SiteMeshContext;
import org.sitemesh.content.ContentProperty;
import org.sitemesh.content.tagrules.TagRuleBundle;
import org.sitemesh.content.tagrules.html.ExportTagToContentRule;
import org.sitemesh.tagprocessor.State;

/**
* Created by Erma on 2017/4/13.
*/
public class MySiteMeshFilter implements TagRuleBundle {

public void install(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
state.addRule("myTag", new ExportTagToContentRule(siteMeshContext,contentProperty.getChild("myTag"),false));
}

public void cleanUp(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {

}
}

4 配置模板

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<br> <sitemesh:write property='title'/><br>
<sitemesh:write property='head'/>

我是装饰器 : title的内容在这里 ~ <sitemesh:write property='title'/>

我是装饰器 : body的内容在这里 ~ <sitemesh:write property='body'/>

我是装饰器 : myTag的内容在这里 ~ <sitemesh:write property='myTag'/>


5 home.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


我是标题~


我是自定义的tag

嗨喽嗨喽 ~

6 效果

posted @ 2017-04-13 16:44  Erma_Jack  阅读(4552)  评论(1)    收藏  举报