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中添加依赖 ~

<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.0</version>
</dependency>

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

<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>

四 sitemesh 3 的配置

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

1 XML配置详解

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

<sitemesh>

    <!-- 默认的装饰路径。如果没有配置其他的路径将启用默认路径,这个可以适用于所有路径 -->
<!-- Map default decorator. This shall be applied to all paths if no other paths match. -->
<mapping decorator="/default-decorator.html"/> <!-- 配置装饰器的路径 -->
<!-- Map decorators to path patterns. -->
<mapping path="/admin/*" decorator="/another-decorator.html"/>
<mapping path="/*.special.jsp" decorator="/special-decorator.html"/> <!-- 对同一路径配置多个装饰器 -->
<!-- Alternative convention. This is more verbose but allows multiple decorators
to be applied to a single path. -->
<mapping>
<path>/articles/*</path>
<decorator>/decorators/article.html</decorator>
<decorator>/decorators/two-page-layout.html</decorator>
<decorator>/decorators/common.html</decorator>
</mapping> <!-- 配置 不被装饰 的路径 -->
<!-- Exclude path from decoration. -->
<mapping path="/javadoc/*" exclue="true"/>
<mapping path="/brochures/*" exclue="true"/> <!-- 默认情况下,
sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰,
我们也可以添加更多的 mime 类型 -->
<mime-type>text/html</mime-type>
<mime-type>application/vnd.wap.xhtml+xml</mime-type>
<mime-type>application/xhtml+xml</mime-type> <!--
Sitemesh 3 默认只提供了 body,title,head 种 tag 类型
我们可以添加自定义的tag规则 -->
<content-processor>
<tag-rule-bundle class="com.something.CssCompressingBundle" />
<tag-rule-bundle class="com.something.LinkRewritingBundle"/>
</content-processor> </sitemesh>

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

<sitemesh>

    <!-- 拦截任何路径配置装饰器 -->
<mapping path="/*" decorator="/WEB-INF/jsp/layouts/decorator.jsp"/> <!-- 这里配置自定义的tag -->
<content-processor>
<tag-rule-bundle class="com.erma.common.MySiteMeshFilter"/>
</content-processor> </sitemesh>

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" %>
<html>
<head>
<title>
<sitemesh:write property='title'/>
</title>
<sitemesh:write property='head'/>
</head>
<body> 我是装饰器 : title的内容在这里 ~ <sitemesh:write property='title'/><br/> 我是装饰器 : body的内容在这里 ~ <sitemesh:write property='body'/><br/> 我是装饰器 : myTag的内容在这里 ~ <sitemesh:write property='myTag'/><br/> </body>
</html>

5 home.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>我是标题~</title>
</head>
<body> <myTag>
我是自定义的tag
</myTag> 嗨喽嗨喽 ~
</body>
</html>

6 效果

最新文章

  1. 如何在ASP.NET Core中实现一个基础的身份认证
  2. SQL Server 2005 安装图解教程(Windows)
  3. iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话,iTunes,iBooks )
  4. Eclipse中文注释乱码解决
  5. Hive笔记--sql语法详解及JavaAPI
  6. 〔写在OS边上〕定性note
  7. 【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees
  8. SpringMVC的视图解析器
  9. 《Windows驱动开发技术详解》之驱动程序的同步处理
  10. OC分类(Category)
  11. 使用mybatis assembly插件打成tar包,在linux系统中运行服务
  12. C# 动态输出Dos命令执行结果
  13. 【译】10. Java反射——数组
  14. P1903 [国家集训队]数颜色 / 维护队列
  15. 20个最常用的Windows命令行
  16. 修改oracle为归档模式
  17. 找不到&quot;javax.servlet.annotation.WebServlet&quot;解决方法
  18. Qt setMargin()和setSpacing() 的含义
  19. python type metaclass
  20. BZOJ3707 圈地

热门文章

  1. RAC之常用方法-----新手入门
  2. jQuery_小测试
  3. Struts2学习笔记④
  4. JDBC整合c3p0数据库连接池 解决Too many connections错误
  5. PHP随机数安全
  6. PHP语言开发微信公众平台(订阅号)之注册
  7. DevOps的几个场景
  8. 【js数据结构】栈解决括号不匹配问题
  9. Spring报错——Scope &#39;session&#39; is not active for the current thread
  10. windows phone 8.1开发:锁屏提醒