sitemesh简介

来源:互联网 发布:酷家乐装修设计软件 编辑:程序博客网 时间:2024/06/05 18:26

Sitemesh 是由一个基于 Web 页面布局、装饰及与现存 Web 应用整合的框架。它能帮助我们由大量页面工程的项目中创建一致的页面布局和外观,如一 致的导航条、一致的 banner 、一致的版权等。它不仅能处理动态的内容,如 JSP 、PHP 、 ASP 、 CGI 等产生的内容,还能处理静态的内容,比如 HTML 的内容,使得它的内容也符合你的页面结构的要求。甚至它能像 include 那样将 HTML 文件作为一个面板的形式嵌入到别的文件中去。它的主要思想是装饰模式。

前提:依赖于 >=Servlet 2.3 版本里引入的新功能 —— 过滤器 (Filters) 。

好处:

使用 sitemesh 给我们带来的是不仅仅是页面结构问题,它的出现让我们有更多的时间去关注底层业务逻辑,而不是整个页面的风格和结构。它让我们摆脱了大量用 include 方式复用页面尴尬局面,它也提供了很大的灵活性以及给我们提供了整合异构 Web 系统页面的一种方案。

第一步:拷贝sitemesh-2.4.jar到工程中
第二步:在web.xml中配置com.opensymphony.sitemesh.webapp.SiteMeshFilter

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

第三步:创建并编辑sitemesh配置文件decorators.xml

<?xml version="1.0" encoding="UTF-8"?><decorators defaultdir="/WEB-INF/sitemesh/">    <decorator name="main" page="basic.jsp">        <pattern>/*</pattern>    </decorator></decorators>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

附一个decorators.xml 配置文件的讲解:(可根据自己实际情况编写装饰规则)

<? xml version = "1.0" encoding = "utf-8" ?>< decorators defaultdir = "/WEB-INF/decorators" ><!—- 定义一个绝对路径 —>    <!-- excludes 标签标示不需要进行装饰的 url -- >    < excludes >        < pattern > /css /* </ pattern >        < pattern > /js /* </ pattern >        < pattern > /images/* </ pattern >        < pattern > /buyer/viewmerchantproduct * </ pattern >        < pattern > /buyer/toindex.do* </ pattern >              < pattern > /buyer/buyerlogout.do </ pattern >              < pattern > */admin -ajax /* </ pattern >        < pattern > */merchant/tomerchantlogin.do* </ pattern >        < pattern > */admin /toadminlogin.do* </ pattern >        < pattern > */admin /adminlogin.do* </ pattern >        < pattern > /merchant/info/login.do* </ pattern >        < pattern > /merchant/report/* </ pattern ></ excludes ><!—- 以下是定义一些模板 主要包括模板的 name 和 page -->      < decorator name = "adminmain" page = "adminmain.jsp" >        < pattern > /admin /* </ pattern >    </ decorator >    < decorator name = "buyerbackstagemain" page = "buyerbacktage.jsp" >        < pattern > /buyer/backstage/* </ pattern >    </ decorator >    < decorator name = "seobuyermain" page = "seobuyermain.jsp" >        < pattern > /buyer/viewcategoryproduct.do* </ pattern >        < pattern > /buyer/viewrootcategory.do* </ pattern >        < pattern > /buyer/viewstandproductdetail.do* </ pattern >        < pattern > /buyer/viewproductreview.do* </ pattern >    </ decorator >    < decorator name = "buyermain" page = "buyermain.jsp" >        < pattern > /admin /previewproduct /* </ pattern >        < pattern > /buyer/* </ pattern >        < pattern > /adminpreview /previewbuyerhelpatricle.do </ pattern >        < pattern > /adminpreview /previewbuyerupdateatricle.do </ pattern >    </ decorator ></ decorators >
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

注意:模板的名字不要相同 , 如果相同错是不会报,只会被最后的那个相同名字的模板装饰。

url 拦截优先级:主要是配备的越精确优先级就高,不是谁在前面谁的优先级就高。

第四步:创建并编辑/WEB-INF/sitemesh/basic.jsp

<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%><h1>User Manager</h1><hr/><decorator:body/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

sitemesh 标签讲解:
<decorator:title default="Welcome to test sitemesh!" /> :读取被装饰页面的标题,并给出了默认标题。
<decorator:head /> :读取被装饰页面的 中的内容;
<decorator:body /> :读取被装饰页面的 中的内容;
当然还有很多的标签,这里不再详细描述。

原创粉丝点击