一,基本概念
1,Sitemesh是一种页面装饰技术 :
1 :它通过过滤器(filter)来拦截页面访问
2 :根据被访问页面的URL找到合适的装饰模板
3 :提取被访问页面的内容,放到装饰模板中合适的位置
4 :最终将装饰后的页面发送给客户端。
2,在sitemesh中,页面分为两种:装饰模板和普通页面。
1)装饰模板,是指用于修饰其它页面的页面。
2)普通页面,一般指各种应用页面。
3,接下来,我们通过一个简单的例子来说明一下sitemesh修饰网页的基本原理。
二,模板修饰网页的原理
通过Sitemesh的注册机制,告诉Sitemesh,当访问该路径时使用XXX模板(假定使用前面那个模板)来修饰被访问页面。

当用户在左边导航栏点击“戏说长城”( /ShowGreatWall.do)时,右边的“戏说长城”页面将会被指定的模板修饰

总结上面过程,Sitemesh修饰网页的基本原理,可以通过下面来说明:

三,Sitemesh的配置与使用
1)WEB-INF/web.xml中加入filter定义与sitemesh的taglib定义
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
</taglib>
2)创建WEB-INF/decorators.xml,在该文件中配置有哪些模板,以及每个模板具体修饰哪些URL,另外也可以配置哪些URL不需要模板控制 , decorators.xml的一个例子如下:
<excludes>
<pattern>/Login*</pattern>
</excludes>
<decorators defaultdir="/decorators">
<decorator name="main" page=“DecoratorMainPage.jsp">
<pattern>/*</pattern>
</decorator>
<decorator name=“pop" page=“PopPage.jsp">
<pattern>/showinfo.jsp*</pattern>
<pattern>
/myModule/GreatWallDetailAction.do*
</pattern>
</decorator>
</decorators>
3)我们看一个修饰模板的例子
<%@page contentType="text/html;?charset=GBK"%>
<%@taglib uri="sitemesh-decorator"?prefix="decorator" %>
<html>
<head>
<title> <decorator:title/> </title>
<decorator:head/>
</head>
<body>
Hello World <hr/>
<decorator:body/>
</body>
</html>
4)我们看一个被修饰的页面的例子:
代码如下:
<%@ page contentType="text/html;?charset=GBK"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>Decorated page goes here.</p
</body>
</html>
5)我们看一下装饰模板中可以使用的Sitemesh标签
<decorator:head />
取出被装饰页面的head标签中的内容。
<decorator:body />
取出被装饰页面的body标签中的内容。
<decorator:title default="" />
取出被装饰页面的title标签中的内容。default为默认值
<decorator:getProperty property="" default="" writeEntireProperty=""/>
取出被装饰页面相关标签的属性值。
writeEntireProperty表明,是显示属性的值还是显示“属性=值”
Html标签的属性
Body标签的属性
Meta标签的属性
注意如果其content值中包含“>或<”会报错,需转码,例如<等等
default是默认值
<decorator:usePage id="" />
将被装饰页面构造为一个对象,可以在装饰页面的JSP中直接引用。
6)看一个在装饰模板中使用标签的例子
<html lang=“ <decorator:getProperty property=‘lang’/> ”>
<head>
<title> <decorator:title default=“你好” /> </title>
<decorator:head />
</head>
<body <decorator:getProperty property=“body.onload" writeEntireProperty=“1"/> >
从meta中获取变量company的名称:
<decorator:getProperty property=“meta.company”/>
下面是被修饰页面的body中的内容:
<decorator:body />
<decorator:usePage id=“myPage" />
<%=myPage.getRequest().getAttribute(“username”)%>
</body>
</html>
7)看一下相应的在被修饰页面中的代码:
<html lang=“en”>
<head>
<title>我的sitemesh</title>
<meta name=“company” content=“smartdot”/>
<meta name=“Author” content=“zhangsan”/>
<script>
function count(){return 10;}
</script>
</head>
<body οnlοad=“count()”>
<p>这是一个被修饰页面</p>
</body>
</html>
四,总结
1,Sitemesh最为重要的就是做用于修饰的模板,并在decorators.xml中配置这些模板用于修饰哪些页面。因此使用Sitemesh的主要过程就是: 做装饰模板,然后 在decorators.xml中配置URL Pattern
2,分析整个工程,看哪些页面需要抽象成模板,例如二级页面、三级页面、弹出窗口等等可能都需要做成相应的模板,一般来说,一个大型的OA系统,模板不会超过8个。
— — —— — — — — — — — — — — — — — — — — — — — — —
如果某个特殊的需求请求路径在过滤器的范围内,但又不想使用模板怎么办?
你总不能这么不讲道理吧!
大家放心吧,SiteMesh早就考虑到这一点了,上面第5步说道的decorators.xml这个时候就起到作用了!
下面是我的decorators.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/decorators">
<!-- Any urls that are excluded will never be decorated by Sitemesh -->
<excludes>
<pattern>/index.jsp*</pattern>
<pattern>/login/*</pattern>
</excludes>
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
decorators.xml有两个主要的结点:
decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板
excludes结点则指定了哪些路径的请求不使用任何模板
如上面代码,/index.jsp和凡是以/login/开头的请求路径一律不使用模板;
另外还有一点要注意的是:decorators结点的defaultdir属性指定了模板文件存放的目录;
以上来自:http://www.jb51.net/web/70208.html
实例:
web.xml
<!-- 配置sitemesh过滤器 -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
decorators.xml
<?xml version="1.0" encoding="utf-8"?>
<pre name="code" class="html"><!-- 定义装饰器模板位置-->
<decorators defaultdir="/WEB-INF/view/decorators">
<pre name="code" class="html"><!-- 定义不需要过滤的请求路径-->
<excludes>
<pattern>*load*</pattern>
<pattern>/services/*</pattern>
<pattern>/restful/*</pattern>
<pattern>/devsupport/*</pattern>
</excludes>
<pre name="code" class="html"><!--定义装饰器 -->
<decorator name="html_body" page="html_body.jsp" />
<decorator name="html_head" page="html_head.jsp" />
<decorator name="html_title" page="html_title.jsp" />
<decorator name="main" page="layout_main.jsp">
<pattern>*.do</pattern> <!--定义要过滤的请求路径 -->
</decorator>
</decorators>
</pre><pre>
common.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<%@ taglib uri="/WEB-INF/tlds/positionConvert.tld" prefix="posC"%>
<%@ taglib uri="/WEB-INF/tlds/EEMTag.tld" prefix="eem"%>
<%@ taglib uri="/WEB-INF/tlds/ECSTag.tld" prefix="ecs"%>
<c:set value="${pageContext.request.contextPath}" var="contextPath" />
jsAndCss4Decorator.jsp
<!-- 专用于使用装饰器的页面公共类 -->
<%@ page pageEncoding="utf-8"%>
<%@ include file="../includes/common.jsp"%>
<html>
<head>
<!-- 系统样式 -->
<link rel="stylesheet" href="${contextPath }/css/main.css" />
<script type="text/javascript" src="${contextPath }/js/jquery-1.10.2.min.js"></script>
<!-- easyui -->
<link rel="stylesheet"
href="${contextPath }/js/jquery-easyui-1.3.4/themes/gray/easyui.css" />
<link rel="stylesheet"
href="${contextPath }/js/jquery-easyui-1.3.4/themes/icon.css" />
<script type="text/javascript"
src="${contextPath }/js/jquery-easyui-1.3.4/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${contextPath }/js/jquery-easyui-1.3.4/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${contextPath }/js/headmenu.js"></script>
<%@ include file="ecsJsAndCss.jsp"%>
<script type="text/javascript">
//设置弹出框位置
openDialogHelper.setOperner();
</script>
</head>
<body></body>
</html>
html_title.jsp
<%@ include file="../includes/common.jsp"%>
<decorator:title />
html_head.jsp
<%@ include file="../includes/common.jsp"%>
<decorator:head/>
html_body.jsp
<%@ include file="../includes/common.jsp"%>
<decorator:body />
layout_main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../includes/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<page:applyDecorator name="html_head" page="/WEB-INF/view/includes/jsAndCss4Decorator.jsp" />
<decorator:head />
<span style="font-size:12px;"><code><span></span><span class="comments"><!-- 从被装饰页面获取title标签内容--></span><span> </span></code></span>
<title><decorator:title /></title>
</head>
<body>
<span style="font-size:12px;"><code><span></span><span class="comments"><!-- 从被装饰页面获取body标签内容 --></span><span> </span></code></span>
<div class="ui-north">
<page:applyDecorator name="html_body" page="/WEB-INF/view/includes/layout_menu.jsp" />
</div>
<decorator:body />
<div class="ui-south">
<page:applyDecorator name="html_body" page="/WEB-INF/view/includes/layout_footer.jsp" />
</div>
</body>
</html>