Sitemesh的使用

今天学习了sitemesh的使用,现在将使用方法记录如下:

sitemesh是opensymphony团队开发的j2ee应用框架之一,旨在提高页面的可维护性和复用性。opensymphony的另一个广为人知的框架为webwork是用作web层的表示框架。

一、Sitemesh是一种页面装饰技术 :
 1  它通过过滤器(filter)来拦截页面访问
 2  根据被访问页面的URL找到合适的装饰模板
 3  提取被访问页面的内容,放到装饰模板中合适的位置
 4  最终将装饰后的页面发送给客户端。

二、在sitemesh中,页面分为两种:装饰模板和普通页面。
1、装饰模板,是指用于修饰其它页面的页面。
2、普通页面,一般指各种应用页面。


三、使用步骤

1、下载sitemesh的jar包,下载地址请自行百度

2、新建Java web工程,并将上一步下载的sitemesh的jar包copy到WEB-INF/lib目录下

3、修改WEB-INF目录下的web.xml文件,加入如下内容:

<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>
4、在WEB-INF目录下新建一个decorators.xml文件,该文件是sitemesh的配置文件,主要用来配置装饰器的路径、过滤的模式以及排除的页面,在该文件中加入如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
	<!-- 指定装饰器页面和需要被装饰的页面 -->
	<decorator name="main" page="decorator.jsp">
		<pattern>/*</pattern>
	</decorator>
	
	<!-- 指定不需要被装饰的页面 -->
	<excludes>
		<pattern>/index2.jsp</pattern>
	</excludes>
</decorators>
5、在WebRoot目录下新建decorators文件夹,在其中新建decorator.jsp页面,该页面即上一步中指定的装饰器页面,一旦某个请求被sitemesh过滤,需要被装饰,则会提取被访问的页面中的一些内容,拼装到decorator.jsp页面中,然后返回给客户端,下面是decorator.jsp页面的内容:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%
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="<%=basePath%>">
    
    <title><decorator:title default="默认的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="styles.css">
	-->

  </head>
  	<h1>Hello world!</h1>
  	<decorator:body />
  <body>
  </body>
</html>
6、编写测试页面,测试sitemesh对页面的装饰效果,这里分别编写index.jsp、index2.jsp、index3.jsp三个页面,代码如下:

index.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="<%=basePath%>">
    
    <title>index</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="styles.css">
	-->
  </head>
  
  <body>
    This is index.jsp page <br>
   <p> 访问该页面时,该页面的title和body部分将会提取出来,放到decorator.jsp页面中指定了decorator:title和decorator:body对应的地方</p>
  </body>
</html>
index2.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="<%=basePath%>">
    
    <title>index2</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="styles.css">
	-->
  </head>
  
  <body>
    This is index2.jsp page. <br>
    <p>由于在decorators.xml文件中配置了将该文件排除在外,所以在访问该页面时,不会使用decorator.jsp来修饰</p>
  </body>
</html>
index3.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="<%=basePath%>">
    
	<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="styles.css">
	-->
  </head>
  
  <body>
    This is index3.jsp page. <br>
    <p>该页面会被decorator.jsp页面修饰,但是该页面没有指定title,所以会用decorator.jsp中title标签的default属性指定的默认值</p>
  </body>
</html>
7、测试

下面启动tomcat服务器,在浏览器中分别访问上面的三个测试页面,结果如下:

index.jsp:



index2.jsp:



index3.jsp:



8、总结

从上面的例子可以看出,sitemesh的工作原理,是将被访问的页面中的某些元素(如title部分,body部分)提取出来,然后放到装饰页(上面的例子中的decorator.jsp)中对应的部分,最后将拼装后的页面返回给客户端,从而达到使用一个页面装饰多个页面的目的,例如很多网站的页面中都有公共的header和footer部分,就可以将header和footer放到装饰页中,然后设置哪些页面需要被装饰,这样就将多个网页加上了header和footer,不仅避免了在多个网页中使用include加入header和footer,而且让网站达到了页面统一的效果。

demo下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yubo_725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值