今天看到了一个新的东西sitemesh,自己用了一下,感觉真的还不错,它把GOF中的decroator模式充分体现出来了,感觉这个给我们大量的jsp加入include省去了麻烦,而这个也相当于我们在这个decorates/main.jsp中定义了一个模板,C#中有一个母版,但是其他的aspx要想用到这个母版就还得选中这个master(母版),但是如果母版中的模块变了,那个子模块也就会出现问题,也就是说子模块与母版之间的耦合比较大,但是用sitemesh不会出现这个的问题,因为在main.jsp中它的只画分了三块,header,body,footer我们只是用sitemesh来把我们的头部与底部进行处理,比较方便。以后就不用每个子页面中加入include,的确少了很多事。
下面是我做的一个demo:
1、下载sitemesh-2.4.2.jar 官方网址:http://wiki.sitemesh.org/display/sitemesh/Home
2、配置web.xml,用来过滤sitemesh
<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>
<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>
3、配置sitemesh
在WEB-INF中写一个decorators.xml
<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
<!-- 此处用来定义不需要过滤的页面 -->
<excludes>
<pattern>/second.jsp</pattern>
</excludes>
<!-- 用来定义装饰器要过滤的页面 -->
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
4、配置模板main.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"
prefix="decorator"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 从被装饰页面获取title标签内容,并设置默认值-->
<title><decorator:title default="默认title" /></title>
<!-- 从被装饰页面获取head标签内容 -->
<decorator:head />
</head>
<body>
<h2>
SiteMesh装饰header
</h2>
<hr />
<!-- 从被装饰页面获取body标签内容 -->
<decorator:body />
<hr />
<h2>
SiteMesh装饰footer
</h2>
</body>
</html>
5、自己随便写一个jsp,其他什么东西也不写,运行tomcat,输入网址一看就晓得是怎么回事了。