首页 > 代码库 > sitemesh学习笔记(2)

sitemesh学习笔记(2)

之前我也是通过网上一些资料来学习sitemesh的,后来发现那些资料都比较老了,现在最近的已经是sitemesh3了而我之前看的是sitemesh2.3,今天重新去看了一些sitemesh3的资料,发现和2.3还是有点区别的。先不讲怎么使用把,先附上一图给小伙伴们。

这张图是不是很清晰地表现了sitemesh的工作流程啊,我觉得是很清晰了,结合我昨天讲的一看就懂了。

 

简单地再阐述下工作原理:

SiteMesh是基于Servlet的filter的,即过滤流。它是通过截取reponse,并进行装饰后再交付给客户。

其中涉及到两个名词: 装饰页面(decorator page)和 “被装饰页面(Content page)" , 即 SiteMesh通过对Content Page的装饰,最终得到页面布局和外观一直的页面,

并返回给客户

sitemesh3.0运行环境需要:servlet2.5, JDK1.5 以上。

 

下面就开始做一个简单的demo吧,首先先去下个sitemesh-3.0-alpha-2.jar包 给个地址:https://github.com/sitemesh/sitemesh3/downloads

下个完整版的,然后在sitemesh-3.0-alpha-2\sitemesh-3.0-alpha-2\dist有这个jar包,看清楚了是dist下面的sitemesh-3.0-alpha-2.jar,不是src下面的那个jar包,名字很象的,刚开始我就弄错了。

打开myeclipse,新建一个工程sitemesh1,把sitemesh-3.0-alpha-2.jar拖到lib下面,在WEB-INF下面新建一个web.xml,用来配置一个filter,

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

这个配置和2.3相比就是类包名不一样,其他都是差不多的。配好了过滤装置,接下来配置装饰页面:WEB-INF下面建一个sitemesh3.xml

这里的配置就有讲究了。容我细细道来

<sitemesh> <!-- 根目录下的所有页面让 decorator.html装饰--> <mapping path="/*" decorator="/decorator/decorator.jsp" /></sitemesh>

如果我们有一张页面不需要被装饰,那么可以这样定义:

<mapping path="/javadoc/page.html" exclue="true"/>

 

也就是说在javadoc文件夹下面的page.html文件不会被装饰

如果想要整个文件夹下的页面都不被装饰可以用*代替:

<mapping path="/javadoc/*" exclue="true"/>

 如果我们想要javadoc文件夹下的文件被多个装饰页面装饰,可以这样配置:

<mapping>      <path>/javadoc/*</path>          <decorator>/decorators/article.html</decorator>      <decorator>/decorators/two-page-layout.html</decorator>      <decorator>/decorators/common.html</decorator></mapping> 

这里javadoc下的所有页面被三个装饰页面装饰。我的demo做了第一个配置,所以我在WebRoot下面建立了decorator文件夹,在文件夹下面建立了一个decorator.jsp作为装饰页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="http://www.mamicode.com/">        <title><sitemesh:write property="title"/></title>    	<meta http-equiv="pragma" content="no-cache">	<meta http-equiv="cache-control" content="no-cache">	<meta http-equiv="expires" content="0">    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">	<meta http-equiv="description" content="This is my page">	<!--	<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">	-->  </head>    <body>  <h1>开头</h1>  	<sitemesh:write property="body"/>  <h1>结尾</h1>  </body></html>

  

获取了被装饰页面的body放在开头和结尾中间。

myeclipse下自动帮你建立了一个index.jsp. 里面body内容默认是This is my JSP page,我们稍微修改一下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="http://www.mamicode.com/">        <title>Hello,sitemesh3</title>	<meta http-equiv="pragma" content="no-cache">	<meta http-equiv="cache-control" content="no-cache">	<meta http-equiv="expires" content="0">    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">	<meta http-equiv="description" content="This is my page">	<!--	<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">	-->  </head>    <body>  	好久不见  </body></html>

  

所以我们的小demo就做好了,打开浏览器

 

轻轻松松合并title和body,神器啊