@dzhai
2016-06-22T13:55:17.000000Z
字数 3070
阅读 2131
Java
Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter
!
项目demo基于sitemesh-3.0.1
<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.1</version>
</dependency>
<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>
sitemesh3提供2种配置方式 java和xml,具体请看Configuring SiteMesh 3
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
<!--默认情况下, 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>
<!-- 默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰 -->
<mapping decorator="/defaulmain.jsp"/>
<!-- 指明满足"*.jsp"的页面,将被"main.jsp"所装饰 -->
<mapping path="*.jsp" decorator="/views/sitemesh3/main.jsp" />
<!-- 指明满足"/exclude.jsp*"的页面,将被排除,不被装饰 -->
<mapping path="/exclude.jsp" exclue="true" />
<!-- 自定义标签 -->
<content-processor>
<tag-rule-bundle class="com.github.dzhai.sitemesh.tag.HeaderTagRuleBundle" />
<tag-rule-bundle class="com.github.dzhai.sitemesh.tag.FooterTagRuleBundle" />
</content-processor>
</sitemesh>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title><sitemesh:write property='title' /> -sitemesh</title>
<sitemesh:write property='head' />
</head>
<body>
<header>header - <sitemesh:write property='header' /></header>
<sitemesh:write property='body' />
<footer>footer - <sitemesh:write property='footer' /></footer>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<title>index页面标题</title>
<script type="text/javascript">
alert('script');
</script>
</head>
<body>
<header>my header</header>
<h2>Hello World!</h2>
<footer>my footer</footer>
</body>
</html>
FooterTagRuleBundle
public class FooterTagRuleBundle implements TagRuleBundle {
@Override
public void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
defaultState.addRule("footer",
new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("footer"), false));
}
@Override
public void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
}
}
HeaderTagRuleBundle
public class HeaderTagRuleBundle implements TagRuleBundle {
@Override
public void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
defaultState.addRule("header",
new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("header"), false));
}
@Override
public void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
}
}
!此处输入图片的描述
生成html源码
<!DOCTYPE html>
<html>
<head>
<title>index页面标题 -sitemesh</title>
<script type="text/javascript">
alert('script');
</script>
</head>
<body>
<header>header - my header</header>
<h2>Hello World!</h2>
<footer>footer - my footer</footer>
</body>
</html>