sitemesh总结小记

一个网站,比如校内网,前端展示的页面总是有固定不变的地方,它们是:上部,左部,和底部,真正变化的内容,都是在中间产生的。
如果用jsp实现这种效果当然是可以的,到处是指令标签
<%@includefile="header.jsp" %> <%@includefile="footer.jsp" %>

这样写起来分麻烦,而且一旦改动哪里,为了全局保持统一,到处都要改。
为此,需要用一种好的解决方法,这就是SiteMesh框架,SiteMesh是基于Java、J2EE和XML的开源框架,依赖于从Servlet 2.3版本里引入的新功能——过滤器(Filters),它的主要思想是装饰设计模式,把变化的和不变的分离开来,用不变的去修饰各种变化的内容。

1.下载sitemesh-2.3.jar,当前最新版本好像就是2.3把,放到lib包下
2.在web.xml中添加:
 <filter>
<filter-name>sitemesh</filter-name>
<filterclass>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<servlet-name>dispatcher</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>



3.在WEB-INF下面建立文件夹decorators,在decorators中新建几个jsp文件,多少取决于你的需要,我建立了3个,default.jsp,header.jsp和footer.jsp,其中default.jsp运用
<%@includefile="header.jsp" %> <%@includefile="footer.jsp" %>
指令标签,将两外2个包进去了,default.jsp现在就做为静态不变的部分了。用来修饰变化的部分,其中default.jsp的代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><decorator:title/> - xxxx网站</title>
<decorator:head/>
</head>
<body class="<decorator:getProperty property="meta.class"/>" id="<decorator:getProperty property="meta.id"/>">
<c:set var="channel"><decorator:getProperty property="meta.channel"/></c:set>
<div>
<%@include file="header.jsp" %>

<decorator:body/>

<%@ include file="footer.jsp" %>
</div>
</body>
</html>


4.在WEB-INF下面建立文件decorators.xml,他是用来定义装饰页面的位置和装饰页面应当应用到哪些jsp中的。代码如下

<decorators defaultdir="/WEB-INF/decorators">
<decorator name="default" page="default.jsp">
<pattern>/*</pattern>
<pattern>/*.jsp</pattern>
</decorator>
</decorators>


5.在WEB-INF下面建立文件sitemesh.xml,用来告诉框架,什么类型需要用到什么解析器,用到什么映射器,代码如下:

<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser default="true" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
<parser content-type="text/html;charset=UTF-8" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
</page-parsers>

<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}"/>
</mapper>
</decorator-mappers>
</sitemesh>


这样基本就完成了。很多自定义的内容,比如content-type="text/html;charset=UTF-8" ,也许你还要解析别的meta类型,那就自己再定义,而且sitemesh有很多不同的解析器和映射器,可以自己选择。

[size=large][b]原理部分[/b][/size]:
1.当web应用程序处理完后,控制返回给SiteMesh的Filter,它会检查web应用生成响应的内容类型(content type),然后基于响应类型,生成不同的解析器来解析响应,这些是在sitemesh.xml中定义的。我所定义的响应类型content-type="text/html"用 HTMLPageParser处理,SiteMesh就会成HTMLPageParser,并把web应用生成的页面传递给它,HTMLPageParser会解析这个页面,提取出这个页面的header、footer、title 等内容。
2.SiteMesh有一个概念,叫做修饰器映射,实现这个概念的接口是DecoratorMapper(有init()和getDecorator()方法)。映射器在sitemesh.xml里声明。在 sitemesh.xml文件里,每一个映射器都是它上一个映射器的父映射。当SiteMesh需要一个修饰器来修饰页面的时候,会在 sitemesh.xml里查找映射器,生成找到的第一个映射器的实例并调用getDecorator()方法,在这个方法里尝试查找针对那个页面的修饰器。如果找到了就返回;否则,调用父映射器的getDecorator()方法,反复进行这个过程,直到找到正确的修饰器。
3.找到修饰器后,SiteMesh会把请求分发给它。修饰器JSP页面(default.jsp)会访问在前阶段里解析出来的页面信息。使用各种SiteMesh自定义标签来提取页面信息不同的部分(比如header、footer和title)并把它们插入到输出文件合适的位置上去。(default.jsp中的<decorator:title/>,<decorator:head/><decorator:body/>等,都是插入信息的地方。)
4.等都插入好了,再发给前端展示出来。你完全不会感受到SiteMesh的存在,配置好了以后,使用就可以了。非常方便
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值