sitemesh是什么?

sitemesh是什么?

百度的答案:

sitemesh

OS(OpenSymphony)的SiteMesh是一个用来在JSP中实现页面布局和装饰(layout and decoration)的框架组件,能够帮助网站开发人员较容易实现页面中动态内容和静态装饰外观的分离。

  Sitemesh功能基础

  Sitemesh是由一个基于Web页面布局、装饰及与现存Web应用整合的框架。它能帮助我们再由大量页面工程的项目中创建一致的页面布局和外观,如一致的导航条、一致的banner、一致的版权等。它不仅能处理动态的内容,如JSP、PHP、ASP、CGI等产生的内容,还能处理静态的内容,比如HTML的内容,使得它的内容也符合你的页面结构的要求。甚至它能像include那样将HTML文件作为一个面板的形式嵌入到别的文件中去。所有的这些,都是GOF的Decorator模式的最生动的实现。装饰模式是在不必改变原类文件和使用集成的情况下,动态地扩展一个对象的功能。它能通过创建一个包装对象,也就是装饰来包裹的对象。尽管它是由Java语言来实现的,但是它能与其他Web应用很好的集成

 

 

javaeye 的举例:

web开发当中,前端的页面逻辑很难被重用,当我们在每一个页面中用include来复用公共的header, css, js,footer时,会大量的出现重复的代码,无形中增加的开发人员的负担.sitemesh通过filter截取request和response,并给原始的页面加入一定的装饰(可能为header,footer...),然后把结果返回给客户端,并且被装饰的原始页面并不知道sitemesh的装饰,这也就达到了脱耦的目的。

SiteMesh 是opensymphony项目,下是官网中的介绍:

SiteMesh is a web-page layout and decoration framework and web- application integration framework to aid in creating large sites consisting of many pages for which a consistent look/feel, navigation and layout scheme is required.

SiteMesh intercepts requests to any static or dynamically generated HTML page requested through the web-server, parses the page, obtains properties and data from the content and generates an appropriate final page with modifications to the original. This is based upon the well-known GangOfFour Decorator design pattern.

SiteMesh can also include entire HTML pages as a Panel within another page. This is similar to a Server-Side Include, except that the HTML document will be modified to create a visual window (using the document's Meta-data as an aid) within a page. Using this feature, Portal type web sites can be built very quickly and effectively. This is based upon the well-known GangOfFour Composite design pattern.

SiteMesh is built using Java 2 with Servlet, JSP and XML technologies. This makes it ideal for use with J2EE applications, however it can be integrated with server-side web architectures that are not Java based such as CGI (Perl/Python/C/C++/etc), PHP, Cold Fusion, etc...

SiteMesh is very extensible and is designed in a way in which it is easy to extend for custom needs.

WEB-INF  下decorator.xml文件

Java代码 sitemesh是什么? - 张自强 - 记录在编程世界里成长的历程

  1. <decorators defaultdir="/WEB-INF/decorators">   
  2.     <!-- 不需要修饰的页面 -->   
  3.     <excludes>   
  4.         <pattern>/css/*</pattern>   
  5.         <pattern>/js/*</pattern>   
  6.         <pattern>/images/*</pattern>   
  7.         <pattern>/dojo/*</pattern>   
  8.         <pattern>/webwork/*</pattern>   
  9.         <pattern>/login.jsp*</pattern>   
  10.          <pattern>/register/*</pattern>   
  11.     </excludes>   
  12.     <decorator name="main" page="main.jsp">   
  13.         <pattern>/*</pattern>   
  14.     </decorator>   
  15.      </decorators>  

<decorators defaultdir="/WEB-INF/decorators"> <!-- 不需要修饰的页面 --> <excludes> <pattern>/css/*</pattern> <pattern>/js/*</pattern> <pattern>/images/*</pattern> <pattern>/dojo/*</pattern> <pattern>/webwork/*</pattern> <pattern>/login.jsp*</pattern> <pattern>/register/*</pattern> </excludes> <decorator name="main" page="main.jsp"> <pattern>/*</pattern> </decorator> </decorators>

defaultdir为装饰页面所在的文件夹.

装饰页面main.jsp,主要的页面结构布局.

代码:

Java代码 sitemesh是什么? - 张自强 - 记录在编程世界里成长的历程

  1. <%@ page contentType="text/html;charset=utf-8" language="java"%>   
  2. [color=red]<%@ taglib uri="sitemesh-decorator" prefix="decorator"%>   
  3. <%@ taglib uri="sitemesh-page" prefix="page"%>[/color]   
  4.   
  5. <%   
  6. String path = request.getContextPath();   
  7. %>   
  8. <HTML>   
  9.     <HEAD>   
  10.     <TITLE><decorator:title default="main page" /></TITLE>   
  11.   
  12.         <decorator:head />   
  13.     <link rel="stylesheet" type="text/css"  
  14.             href="<%=path%>/css/default.css" />   
  15.         <link rel="stylesheet" type="text/css" href="<%=path%>/css/tab.css" />   
  16.         <script language="javascript" src="<%=path%>/js/formControl.js"></script>   
  17.         <script language="javascript" src="<%=path%>/js/changePage.js"></script>   
  18.         <META http-equiv=ImageToolbar content=no>   
  19.     </HEAD>   
  20.     <BODY id=main>   
  21.         <jsp:include flush="true" page="/commont/header.jsp"></jsp:include>   
  22.         <DIV id=container>   
  23.             <DIV id=content style="height:500px">   
  24.                 <decorator:body />            <DIV id=navigation>   
  25.                     <A accessKey=3 name=menu></A>   
  26.                     <H2 class=hide>   
  27.                         Navigation   
  28.                     </H2>   
  29.                     <UL id=menuroot>   
  30.                         <LI>   
  31.                             <A title="index" accessKey=1 href="<%=path%>/index.jsp">Index</A>   
  32.                         </LI>   
  33.                         <LI>   
  34.                             <A title="ListUser" accessKey=3  
  35.                                 href="<%=path%>/user/listUsers.action">ListUser</A>   
  36.                         </LI>   
  37.                         <LI>   
  38.                             <A title="listWorkSum" accessKey=4  
  39.                                 href="<%=path%>/worksum/listWorkSums.action">listWorkSum</A>   
  40.                         </LI>   
  41.     <LI>   
  42.                             <A title="ListUser" accessKey=3  
  43.                                 href="<%=path%>/clickstream/clickstreams.jsp">ClickStream</A>   
  44.                         </LI>   
  45.                         <LI>   
  46.                             <A title="monitor" accessKey=6  
  47.                                 href="<%=path%>/monitor/jamonadmin.jsp">monitor</A>   
  48.                         </LI>   
  49.                         <LI>   
  50.                             <A title="workflow" accessKey=6  
  51.                                 href="<%=path%>/workflow/default.jsp">workflow</A>   
  52.                         </LI>   
  53.                         <LI>   
  54.                             <A title="workflow" accessKey=4  
  55.                                 href="<%=path%>/workflow/workflowLogin.action">workflowAction</A>   
  56.                         </LI>   
  57.                         <LI>   
  58.                             <A title="soap" accessKey=6  
  59.                                 href="<%=path%>/soap/default.jsp">soap</A>   
  60.                         </LI>   
  61.                         <LI>   
  62.                             <A title="Logout" accessKey=5 href="<%=path%>/logout.jsp">Logout</A>   
  63.                         </LI>   
  64.                     </UL>   
  65.                 </DIV>   
  66.   
  67.   
  68.             </DIV>   
  69.             <DIV id=header>   
  70.                 Copyright © 2003-2005 cherubo All Rights Reserved   
  71.             </DIV>   
  72.             <jsp:include page="/commont/footer.jsp" />   
  73.     </BODY>   
  74. </HTML>  

<%@ page contentType="text/html;charset=utf-8" language="java"%>[color=red]<%@ taglib uri="sitemesh-decorator" prefix="decorator"%><%@ taglib uri="sitemesh-page" prefix="page"%>[/color]<%String path = request.getContextPath();%><HTML> <HEAD> <TITLE><decorator:title default="main page" /></TITLE> <decorator:head /> <link rel="stylesheet" type="text/css" href="<%=path%>/css/default.css" /> <link rel="stylesheet" type="text/css" href="<%=path%>/css/tab.css" /> <script language="javascript" src="<%=path%>/js/formControl.js"></script> <script language="javascript" src="<%=path%>/js/changePage.js"></script> <META http-equiv=ImageToolbar content=no> </HEAD> <BODY id=main> <jsp:include flush="true" page="/commont/header.jsp"></jsp:include> <DIV id=container> <DIV id=content style="height:500px"> <decorator:body /> <DIV id=navigation> <A accessKey=3 name=menu></A> <H2 class=hide> Navigation </H2> <UL id=menuroot> <LI> <A title="index" accessKey=1 href="<%=path%>/index.jsp">Index</A> </LI> <LI> <A title="ListUser" accessKey=3 href="<%=path%>/user/listUsers.action">ListUser</A> </LI> <LI> <A title="listWorkSum" accessKey=4 href="<%=path%>/worksum/listWorkSums.action">listWorkSum</A> </LI> <LI> <A title="ListUser" accessKey=3 href="<%=path%>/clickstream/clickstreams.jsp">ClickStream</A> </LI> <LI> <A title="monitor" accessKey=6 href="<%=path%>/monitor/jamonadmin.jsp">monitor</A> </LI> <LI> <A title="workflow" accessKey=6 href="<%=path%>/workflow/default.jsp">workflow</A> </LI> <LI> <A title="workflow" accessKey=4 href="<%=path%>/workflow/workflowLogin.action">workflowAction</A> </LI> <LI> <A title="soap" accessKey=6 href="<%=path%>/soap/default.jsp">soap</A> </LI> <LI> <A title="Logout" accessKey=5 href="<%=path%>/logout.jsp">Logout</A> </LI> </UL> </DIV> </DIV> <DIV id=header> Copyright © 2003-2005 cherubo All Rights Reserved </DIV> <jsp:include page="/commont/footer.jsp" /> </BODY></HTML>

上面页面

以下列着全部标签: Decorator Tags Page Tags

被用于建立装饰器页面. 被用于从原始内容页面访问装饰器.

<decorator:head />

<decorator:body />

<decorator:title />

<decorator:getProperty />

<decorator:usePage />

<page:applyDecorator />

<page:param

 

<decorator:head />

插入原始页面(被包装页面)的head标签中的内容(不包括head标签本身)。

<decorator:body />

插入原始页面(被包装页面)的body标签中的内容。

<decorator:title [ default="..." ] />

插入原始页面(被包装页面)的title标签中的内容,还可以添加一个缺省值。

被修饰的页面

Java代码 sitemesh是什么? - 张自强 - 记录在编程世界里成长的历程

  1. <html>   
  2. <head>   
  3.     <title>main</title>    
  4.   
  5.        
  6. </head>   
  7. <body>   
  8. <div style="PADDING-TOP: 50px;">   
  9. <h1>   
  10. Welcome Into NewiKi System    
  11. </h1>   
  12. <h3>In Newiki System,You Can:</h3>   
  13. <h3>You can do Anything!</h3>   
  14. <h3>It's Life ,Live It!</h3>   
  15. <h3>You Are Freedom!</h3>   
  16.   
  17. </div>   
  18.  </body>   
  19. </html>  

<html><head> <title>main</title> </head><body><div style="PADDING-TOP: 50px;"><h1>Welcome Into NewiKi System </h1><h3>In Newiki System,You Can:</h3><h3>You can do Anything!</h3><h3>It's Life ,Live It!</h3><h3>You Are Freedom!</h3></div> </body></html>

很简单,很清楚的结构.

WEB.XML

Java代码 sitemesh是什么? - 张自强 - 记录在编程世界里成长的历程

  1. <filter>   
  2.     <filter-name>sitemesh</filter-name>   
  3.        <filter-class>   
  4.            com.opensymphony.module.sitemesh.filter.PageFilter   
  5.        </filter-class>   
  6.     </filter>   
  7.         <filter-mapping>   
  8.         <filter-name>sitemesh</filter-name>   
  9.         <url-pattern>*.jsp</url-pattern>   
  10.     </filter-mapping>   
  11.     <filter-mapping>   
  12.         <filter-name>sitemesh</filter-name>   
  13.         <url-pattern>*.action</url-pattern>   
  14.     </filter-mapping>  

 

 

其他的例子:

如果用jsp实现这种效果当然是可以的,到处是指令标签

Java代码

  1. <%@includefile="header.jsp" %> <%@includefile="footer.jsp" %>  

<%@includefile="header.jsp" %> <%@includefile="footer.jsp" %>

这样写起来分麻烦,而且一旦改动哪里,为了全局保持统一,到处都要改。

    为此,需要用一种好的解决方法,这就是SiteMesh框架,SiteMesh是基于Java、J2EE和XML的开源框架,依赖于从Servlet 2.3版本里引入的新功能——过滤器(Filters),它的主要思想是装饰设计模式,把变化的和不变的分离开来,用不变的去修饰各种变化的内容。

1.下载sitemesh-2.3.jar,当前最新版本好像就是2.3把,放到lib包下

2.在web.xml中添加:

   

Xml代码

  1. <filter>  
  2.        <filter-name>sitemesh</filter-name>  
  3.        <filterclass>  
  4.            com.opensymphony.module.sitemesh.filter.PageFilter   
  5.        </filter-class>  
  6.     </filter>  
  7.     <filter-mapping>  
  8.        <filter-name>sitemesh</filter-name>  
  9.        <servlet-name>dispatcher</servlet-name>  
  10.        <dispatcher>REQUEST</dispatcher>  
  11.        <dispatcher>FORWARD</dispatcher>  
  12.     </filter-mapping>  

<filter> <filter-name>sitemesh</filter-name> <filterclass> com.opensymphony.module.sitemesh.filter.PageFilter </filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <servlet-name>dispatcher</servlet-name> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>

3.在WEB-INF下面建立文件夹decorators,在decorators中新建几个jsp文件,多少取决于你的需要,我建立了3个,default.jsp,header.jsp和footer.jsp,其中default.jsp运用

Java代码

  1. <%@includefile="header.jsp" %> <%@includefile="footer.jsp" %>  

<%@includefile="header.jsp" %> <%@includefile="footer.jsp" %>指令标签,将两外2个包进去了,default.jsp现在就做为静态不变的部分了。用来修饰变化的部分,其中default.jsp的代码如下:

Java代码

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>   
  2. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">   
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">   
  5. <head>   
  6.      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   
  7.      <title><decorator:title/> - xxxx网站</title>   
  8.      <decorator:head/>   
  9. </head>   
  10. <body class="<decorator:getProperty property="meta.class"/>" id="<decorator:getProperty property="meta.id"/>">   
  11. <c:set var="channel"><decorator:getProperty property="meta.channel"/></c:set>   
  12. <div>   
  13.      <%@include file="header.jsp" %>   
  14.   
  15.      <decorator:body/>   
  16.   
  17.      <%@ include file="footer.jsp" %>   
  18. </div>   
  19. </body>   
  20. </html>  

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title><decorator:title/> - xxxx网站</title> <decorator:head/></head><body class="<decorator:getProperty property="meta.class"/>" id="<decorator:getProperty property="meta.id"/>"><c:set var="channel"><decorator:getProperty property="meta.channel"/></c:set><div> <%@include file="header.jsp" %> <decorator:body/> <%@ include file="footer.jsp" %></div></body></html>

4.在WEB-INF下面建立文件decorators.xml,他是用来定义装饰页面的位置和装饰页面应当应用到哪些jsp中的。代码如下

Xml代码

  1. <decorators defaultdir="/WEB-INF/decorators">  
  2.     <decorator name="default" page="default.jsp">  
  3.         <pattern>/*</pattern>  
  4.         <pattern>/*.jsp</pattern>  
  5.     </decorator>  
  6. </decorators>  

<decorators defaultdir="/WEB-INF/decorators"> <decorator name="default" page="default.jsp"> <pattern>/*</pattern> <pattern>/*.jsp</pattern> </decorator></decorators>

5.在WEB-INF下面建立文件sitemesh.xml,用来告诉框架,什么类型需要用到什么解析器,用到什么映射器,代码如下:

Xml代码

  1. <sitemesh>  
  2.     <property name="decorators-file" value="/WEB-INF/decorators.xml"/>  
  3.     <excludes file="${decorators-file}"/>  
  4.     <page-parsers>  
  5.         <parser default="true" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>  
  6.         <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>  
  7.         <parser content-type="text/html;charset=UTF-8" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>  
  8.     </page-parsers>  
  9.   
  10.     <decorator-mappers>  
  11.         <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">  
  12.             <param name="config" value="${decorators-file}"/>  
  13.         </mapper>  
  14.     </decorator-mappers>  
  15. </sitemesh>  

<sitemesh> <property name="decorators-file" value="/WEB-INF/decorators.xml"/> <excludes file="${decorators-file}"/> <page-parsers> <parser default="true" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/> <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/> <parser content-type="text/html;charset=UTF-8" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/> </page-parsers> <decorator-mappers> <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper"> <param name="config" value="${decorators-file}"/> </mapper> </decorator-mappers></sitemesh>

这样基本就完成了。很多自定义的内容,比如content-type="text/html;charset=UTF-8" ,也许你还要解析别的meta类型,那就自己再定义,而且sitemesh有很多不同的解析器和映射器,可以自己选择。

原理部分:

    1.当web应用程序处理完后,控制返回给SiteMesh的Filter,它会检查web应用生成响应的内容类型(content type),然后基于响应类型,生成不同的解析器来解析响应,这些是在sitemesh.xml中定义的。我所定义的响应类型content-type="text/html"用 HTMLPageParser处理,SiteMesh就会成HTMLPageParser,并把web应用生成的页面传递给它,HTMLPageParser会解析这个页面,提取出这个页面的header、footer、title 等内容。

    2.SiteMesh有一个概念,叫做修饰器映射,实现这个概念的接口是DecoratorMapper(有init()和getDecorator()方法)。映射器在sitemesh.xml里声明。在 sitemesh.xml文件里,每一个映射器都是它上一个映射器的父映射。当SiteMesh需要一个修饰器来修饰页面的时候,会在 sitemesh.xml里查找映射器,生成找到的第一个映射器的实例并调用getDecorator()方法,在这个方法里尝试查找针对那个页面的修饰器。如果找到了就返回;否则,调用父映射器的getDecorator()方法,反复进行这个过程,直到找到正确的修饰器。

    3.找到修饰器后,SiteMesh会把请求分发给它。修饰器JSP页面(default.jsp)会访问在前阶段里解析出来的页面信息。使用各种SiteMesh自定义标签来提取页面信息不同的部分(比如header、footer和title)并把它们插入到输出文件合适的位置上去。(default.jsp中的<decorator:title/>,<decorator:head/><decorator:body/>等,都是插入信息的地方。)

    4.等都插入好了,再发给前端展示出来。你完全不会感受到SiteMesh的存在,配置好了以后,使用就可以了。非常方便

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值