`
zhangzuanqian
  • 浏览: 262754 次
  • 来自: ...
社区版块
存档分类
最新评论

SiteMesh 2.X版本的简单使用

阅读更多

SiteMesh 2.X版本的简单使用

 

1. SiteMesh的简介

 

        Sitemesh是由一个基于Web页面布局、装饰及与现存Web应用整合的框架。它能帮助我们再由大量页面工程的项目中创建一致的页面布局和外观,如一 致的导航条、一致的banner、一致的版权等。它不仅能处理动态的内容,如JSP、PHP、ASP、CGI等产生的内容,还能处理静态的内容,比如 HTML的内容,使得它的内容也符合你的页面结构的要求。甚至它能像include那样将HTML文件作为一个面板的形式嵌入到别的文件中去。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and footer,现在,在sitemesh的帮助下,我们可以开心的删掉他

2. SiteMesh的工作原理

 

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

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

并返回给客户

运行环境需要:servlet2.3 , JDK1.4 以上。 

 

 2.1 正常模式下的web访问流程

 

2.2 加入SiteMesh装饰的web访问流程 

 Figure 2

 2.2 SiteMesh配置文件

SiteMesh通过sitemesh.xml 和 decorators.xml配置文件来管理装饰页面以及高级属性的配置。

这两个文件都放置在/WEB-INF下 

3. 搭建SiteMesh环境

 

 3.1 准备资源

 

以上该链接可以获得siteMesh2.4.jar, sitemesh-page.tld , sitemesh-decorator.tld 这个三个必要文件

将jar包复制进/WEB-INF/lib目录下, 两个tld文件导入/WEB-INF下即可

 在web.xml中加入siteMesh的filter和taglib

  <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>/*</url-pattern>
  </filter-mapping> 

 <!-- not required for containers that fully support JSP 1.2 -->
  <taglib>
    <taglib-uri>sitemesh-page</taglib-uri>
    <taglib-location>/WEB-INF/lib/sitemesh-page.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>sitemesh-decorator</taglib-uri>
    <taglib-location>/WEB-INF/lib/sitemesh-decorator.tld</taglib-location>
  </taglib>

 3.2 建立decorators.xml

在/WEB-INF下创建decorators.xml文件,siteMesh通过该文件来获知"装饰页面"和"被装饰页面"的映射 

 decorators.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!-- 默认目录 -->
<decorators defaultdir="/decorators">

    <!-- 缺省装饰页 -->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
     
    <!-- 自定义装饰页,我们下面实例就是这部分起作用 -->
    <decorator name="mai" page="mai.jsp">
        <pattern>/mai.html</pattern>
    </decorator>

    <!-- 只装饰一个页面也可用这种方式定义 -->
    <decorator name="panel" page="panel.jsp"/>

    <!-- 装饰velocity模板 -->
    <decorator name="velocity" page="velocity.vm">
        <pattern>/velocity.html</pattern>
    </decorator>
    
    <!-- 装饰freeMarker模板 -->
    <decorator name="freemarker" page="freemarker.ftl">
        <pattern>/freemarker.html</pattern>
    </decorator>

    <decorator name="test" page="test.jsp">
        <pattern>/agent.jsp</pattern>
    </decorator>
</decorators>

  3.3 装饰页的创建

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

/*这里导入了SiteMesh的标签库 */

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

OK,there is a decorator begin!<hr />
    /*这里的意思是,被装饰页的title内容将会在这里插入 */
    <decorator:title></decorator:title>
  
</head>
<body>
    /*被修饰页的body内容将在这里插入
    <decorator:body></decorator:body>

<hr />Yse,there is a decorator end !

</body>
</html>

 3.4 被修饰页的创建

在web目录(或webContent)下创建mai.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
this is the Content Page !!!
</body>
</html>

 3.5 使用tomcat进行示例运行,访问http://localhost:8080/{your project name}/mai.html , 运行结果如下:

 

转自:http://www.cnblogs.com/mailingfeng/archive/2012/02/20/2296041.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics