??xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- 配置Web应用配置文g的根元素Qƈ指定配置文g的Schema信息 -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 定义ActionContextCleanUpqo?-->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<!-- 定义SiteMesh的核心过滤器 -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<!-- 定义Struts 2的核心过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- 定义qo器链 -->
<!-- 排在W一位的qo器是QActionContextCleanUpqo?-->
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 排在W二位的qo器是QSiteMesh的核心过滤器 -->
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 排在W三位的qo器是QFilterDispatcherqo?-->
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 定义一个ListenerQ该Listener在应用启动时创徏Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 定义一个ListenerQ该Listener在应用启动时加蝲MyFaces的Context -->
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener
</listener-class>
</listener>
<!-- 配置JSF的FacesServletQ让其在应用启动时加?-->
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 让FacesServlet拦截所有以*.actionl尾的请?-->
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
二、ؓ什么要使用SiteMesh?
我们的团队开发J2EE应用的时候,l常会碰C个比较头疼的问题Q由于Web面是由不同的h所开发,所以开发出来的界面通常是千奇百怪,通常让项目管理h员苦W不得。而实际上QQ何一个项目都会要求界面的l一风格和美观,既然风格l一Q那p明UI层肯定有很多可以抽出来共用的静态或动态部分;如何整合q些通用的静态或动态UI呢?Apache Tiles框架站了出来很好的解决了q一问题Q再加上他与struts的完集成,D大小目都把他作为UI层的首选框Ӟ但是QTiles实有着它很多的不之处,下文我会介绍,本文惌的是Q除了Apache Tiles框架,其实我们q有更好的解 x案,那就?SiteMeshQ?br />
本文介绍了一个基于Web面的布局、装C及应用整合的框架SitemeshQ它能帮助你Z的应用创Z致的外观Q很好的取代Apache Tiles;
三、SiteMesh VS Apache Tiles
用过struts的朋友应该对Apache Tiles的不会陌生,我曾l有一文章介l过struts中tiles框架的组合与l承Q现在怎么看怎么觉得复杂
从用角度来看,Tilesg是Sitemesh标签<page:applyDecorator>的一个翻版。其实sitemesh最强的一个特性是sitemeshdecorator模式用在qo器上。Q何需要被装饰的页面都不知道它要被谁装饎ͼ所以它可以用来装璜来自php、asp、CGI{生的面了。你可以定义若干个装饰器Q根据参数动态地选择装饰器,产生动态的外观以满你的需求。它也有一套功能强大的属性体p,它能帮助你构建功能强大而灵zȝ装饰器。相比较而言Q在q方面Tiles逊色许多?nbsp;
个h觉得在团队开发里面,Apache Tiles框架会导致所有h不仅仅要了解q且清楚Apache Tiles的存在,q且要特别熟悉每一个Tiles layout模板的作用,否则可能出现用错模板的情况Q除此之外,每个人涉及到的所有WEB面都需要去配置文g里面逐个配置Q不仅麻烦出错的几率q高Q而以上所有的不都是SiteMesh所不存在的?
四、SiteMesh的基本原?nbsp;
一个请求到服务器后Q如果该h需要sitemesh装饰Q服务器先解释被h的资源,然后Ҏ配置文g
获得用于该请求的装饰器,最后用装饰器装饰被h资源Q将l果一同返回给客户端浏览器?
五、如何用SiteMesh
q里以struts2+spring2+hibernate3构架的系lؓ?nbsp;
1、下载SiteMesh
下蝲地址Qhttp://www.opensymphony.com/sitemesh/download.action 目前的最新版本是Version 2.3Q?nbsp;
2、在工程中引入SiteMesh的必要jar包,和struts2-sitemesh-plugin-2.0.8.jarQ?nbsp;
3、修改你的web.xml,在里面加入sitemesh的过滤器Q示例代码如下:
<!-- sitemesh配置 -->
<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>
注意qo器的位置Q应该在struts2的org.apache.struts2.dispatcher.FilterDispatcherqo器之前org.apache.struts2.dispatcher.ActionContextCleanUpqo器之后,否则会有问题Q?
4、在下蝲的SiteMesh包中扑ֈsitemesh.xmlQ?\sitemesh-2.3\src\example-webapp\WEB-INF目录下就?
其拯?WEB-INF目录下;
5、在sitemesh.xml文g中有一个propertyl点(如下)Q该l点指定了decorators.xml在工E中的位|?让sitemesh.xml能找C; 按照此\径新建decorators.xml文gQ当然这个\径你可以L改变Q只要propertyl点的valueg其匹配就行;
<property name="decorators-file" value="/WEB-INF/sitemesh/decorators.xml"/>
6、在WebRoot目录下新建decorators目录Qƈ在该目录下新Z个模板jspQ根据具体项目风格编辑该模板Q?br />
如下CZQ我的模板:main.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="decorator"
uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
%>
<html>
<head>
<title><decorator:title default="kangxm test" />
</title>
<!-- 面Head由引用模板的子页面来替换 -->
<decorator:head />
</head>
<body id="page-home">
<div id="page-total">
<div id="page-header">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div class="topFunc">
我的账户
|
退?
</div>
</td>
</tr>
</table>
</div>
</div>
<!-- end header -->
<!-- Menu Tag begin -->
<div id="page-menu" style="margin-top: 8px; margin-bottom: 8px;">
<div>
q里放菜?
</div>
</div>
<!-- Menu Tag end -->
<div id="page-content" class="clearfix">
<center>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<decorator:body /><!-- q里的内容由引用模板的子面来替?-->
</td>
</tr>
</table>
</center>
</div>
<!-- end content -->
<div id="page-footer" class="clearfix">
q里N面底?
<!-- end footer -->
</div>
<!-- end page -->
</body>
</html>
q就是个单的模板Q页面的头和脚都由模杉K的静态HTML军_了,主页面区域用的是<decorator:body />标签Q也是说凡是能q入qo器的h生成的页面都会默认加上模板上的头和脚Q然后页面自w的内容自动放?lt;decorator:body />标签所在位|;
<decorator:title default="Welcome to test sitemesh!" />Q读取被装饰面的标题,q给Z默认标题?
<decorator:head />Q读取被装饰面?lt;head>中的内容Q?
<decorator:body />Q读取被装饰面?lt;body>中的内容Q?
7、说到这里大家就要想了,那如果某个特D的需求请求\径在qo器的范围内,但又不想使用模板怎么办? 你M能这么不讲道理吧Q大家放心吧QSiteMesh早就考虑到这一点了Q上面第5步说道的decorators.xmlq个时候就起到作用了!
下面是我的decorators.xmlQ?
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/decorators">
<!-- Any urls that are excluded will never be decorated by Sitemesh -->
<excludes>
<pattern>/index.jsp*</pattern>
<pattern>/login/*</pattern>
</excludes>
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
decorators.xml有两个主要的l点Q?
decoratorl点指定了模板的位置和文件名Q通过pattern来指定哪些\径引用哪个模?
excludesl点则指定了哪些路径的请求不使用M模板
如上面代码,/index.jsp和凡是以/login/开头的h路径一律不使用模板Q?
另外q有一点要注意的是Qdecoratorsl点的defaultdir属性指定了模板文g存放的目?
六、实战感?nbsp;
刚刚做完一个用到sitemesh的项目,跟以前用tiles框架相比Q最大的感受是单,pȝ设计阶段
把模板文g和sitemesh框架搭好了!哪些面使用框架哪些不用,全部都通过UI Demo很快定义出来了Q?
在接下来的开发中所有成员几乎感受不到sitemesh的存在,各自仅仅兛_自己的模块功能实玎ͼ
七、ȝ
使用sitemeshl我们带来的是不仅仅是页面结构问题,它的出现让我们有更多的时间去x底层业务
逻辑Q而不是整个页面的风格和结构。它让我们摆׃大量用include方式复用面尬局面,也避免了tiles
框架在团队开发中的复杂度Q它q提供了很大的灵zL以及给我们提供了整合异构Webpȝ面的一U方?
本文来自CSDN博客Q{载请标明出处Qhttp://blog.csdn.net/it_man/archive/2009/01/17/3817347.aspx