`
li285913050
  • 浏览: 21971 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

SITEMESH页面布局器的使用

阅读更多

使用sitemesh建立复合视图 - 1.hello (作者:chen-neu ,提供给 huihoo.com 发布) 使用sitemesh建立复合视图 - 1.hello <- now

 使用sitemesh建立复合视图 - 2.装饰器

 使用sitemesh建立复合视图 - 3.其它讨论

 sitemesh是opensymphony团队开发的j2ee应用框架之一,旨在提高页面的可维护性和复用性。opensymphony的另一个广为人知的框架为webwork是用作web层的表示框架。他们都是开源的,可以在www.sf.net下找到。

 应用于以下大项目的例子:http://opensource.thoughtworks.com/projects/sitemesh.html www.jboss.org www.theserverside.com www.opensymphony.com www.atlassian.com 简介: sitemesh应用Decorator模式,用filter截取request和response,把页面组件head,content,banner结合为一个完整的视图。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and footer,现在,在sitemesh的帮助下,我们可以开心的删掉他们了。如下图,你想轻松的达到复合视图模式,那末看完本文吧。


   hello sitemesh: 在WEB-INF/web.xml中copy以下filter的定义:<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>
<taglib>
 <taglib-uri>sitemesh-decorator</taglib-uri>
 <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
</taglib>
<taglib>
 <taglib-uri>sitemesh-page</taglib-uri>
 <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
</taglib>
copy所需jar和dtd文件至相应目录,访问opensymphony.sourceforge.net的cvs以获取sitemesh最新版本。
sitemesh.jarWEB-INF/lib sitemesh-decorator.tldWEB-INF
sitemesh-page.tldWEB-INF
建立WEB-INF/decorators.xml描述各装饰器页面(可仿照sitemesh例子)。
<decorators defaultdir="/_decorators">
 <decorator name="main" page="main.jsp">
  <pattern>*</pattern>
 </decorator>
</decorators>


建立装饰器页面 /_decorators/main.jsp <%@ page contentType="text/html;
charset=GBK"%><%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<html>
 <head>
  <title>
   <decorator:title default="装饰器页面..." />
  </title>
  <decorator:head />
 </head>
 <body>
  sitemesh的例子
  <hr>
   <decorator:body />
   <hr>chen56@msn.com
 </body>
</html>


建立一个的被装饰页面 /index.jsp(内容页面) <%@ page contentType="text/html;
charset=GBK"%>
<html>
 <head>
  <title>Agent Test</title>
 </head>
 <body>
  <p>本页只有一句,就是本句.</p>
 </body>
</html>

最后访问index.jsp,将生成如下页面:

 

 


而且,所有的页面也会如同index.jsp一样,被sitemesh的filter使用装饰模式修改成如上图般模样,却不用再使用include标签。


装饰器 decorator概念 建立可复用的web应用程序,一个通用的方法是建立一个分层系统,如同下面一个普通的web应用:
前端,front-end:JSP和Servlets,或jakarta的velocity 控制层框架 Controller :
(Struts/Webwork) 业务逻辑 Business :主要业务逻辑 持久化框架 :hibernate/jdo

可糟糕的是前端的页面逻辑很难被复用,当你在每一个页面中用数之不尽的include来复用公共的header, stylesheet,
scripts,footer时,一个问题出现了-重复的代码,每个页面必须用copy来复用页面结构,而当你需要创意性的改变页面结构时,灾难就爱上了你。

 


sitemesh通过filter截取request和response,并给原始的页面加入一定的装饰(可能为header,footer...),然后把结果返回给客户端,并且被装饰的原始页面并不知道sitemesh的装饰,这也就达到了脱耦的目的。

 


据说即将新出台的Portlet规范会帮助我们标准的实现比这些更多更cool的想法,但可怜的我还不懂它到底是一个什末东东,有兴趣的人可以研究jetspeed,或JSR
(Java Specification Request) 168,但我想sitemesh如此简单,我们不妨先用着。

  让我们看看怎样配置环境 除了要copy到WEB-INF/lib中的sitemesh.jar,
copy到WEB-INF中的sitemesh-decorator.tld,sitemesh-page.tld文件外,还有2个文件要建立到WEB-INF/:
sitemesh.xml (可选) decorators.xml sitemesh.xml 可以设置2种信息: Page Parsers
:负责读取stream的数据到一个Page对象中以被SiteMesh解析和操作。(不太常用,默认即可)

Decorator Mappers :
不同的装饰器种类,我发现2种比较有用都列在下面。一种通用的mapper,可以指定装饰器的配置文件名,另一种可打印的装饰器,可以允许你当用http://localhost/aaa/a.html?printable=true方式访问时给出原始页面以供打印(免得把header,footer等的花哨的图片也搭上)(但一般不用建立它,默认设置足够了:com/opensymphony/module/sitemesh/factory/sitemesh-default.xml):

范例:
<sitemesh>
 <page-parsers>
  <parser default="true"
   class="com.opensymphony.module.sitemesh.parser.DefaultPageParser" />
  <parser content-type="text/html"
   class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
  <parser content-type="text/html;charset=ISO-8859-1"
   class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
 </page-parsers>
 <decorator-mappers>
  <mapper
   class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
   <param name="config" value="/WEB-INF/decorators.xml" />
  </mapper>
  <mapper
   class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
   <param name="decorator" value="printable" />
   <param name="parameter.name" value="printable" />
   <param name="parameter.value" value="true" />
  </mapper>
 </decorator-mappers>
</sitemesh>
decorators.xml :定义构成复合视图的所有页面构件的描述(主要结构页面,header,footer...),如下例:
<decorators defaultdir="/_decorators">
 <decorator name="main" page="main.jsp">
  <pattern>*</pattern>
 </decorator>
 <decorator name="printable" page="printable.jsp" role="customer"
  webapp="aaa" />
</decorators>

defaultdir: 包含装饰器页面的目录 page : 页面文件名 name : 别名 role : 角色,用于安全 webapp :
可以另外指定此文件存放目录 Patterns : 匹配的路径,可以用*,那些被访问的页面需要被装饰。  
最重要的是写出装饰器本身(也就是那些要复用页面,和结构页面)。
其实,重要的工作就是制作装饰器页面本身(也就是包含结构和规则的页面),然后把他们描述到decorators.xml中。
让我们来先看一看最简单的用法:其实最常用也最简单的用法就是我们的hello例子,面对如此众多的技术,我想只要达到功能点到为止即可,没必要去研究太深(除非您有更深的需求)。

<%@ page contentType="text/html; charset=GBK"%><%@ taglib
uri="sitemesh-decorator" prefix="decorator" %>
<html>
 <head>
  <title>
   <decorator:title default="装饰器页面..." />
  </title>
  <decorator:head />
 </head>
 <body>
  sitemesh的例子
  <hr>
   <decorator:body />
   <hr>chen56@msn.com
 </body>
</html>

我们在装饰器页面只用了2个标签:

<decorator:title default="装饰器页面..." />
: 把请求的原始页面的title内容插入到
<title></title>
中间。

<decorator:body />
: 把请求的原始页面的body内的全部内容插入到相应位置。

然后我们在decorator.xml中加入以下描述即可:

<decorator name="main" page="main.jsp">
 <pattern>*</pattern>
</decorator>

这样,请求的所有页面都会被重新处理,并按照main.jsp的格式重新展现在你面前。

  让我们看看更多的用法。(抄袭sitemesh文档) 以下列着全部标签: Decorator TagsPage
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标签中的内容,还可以添加一个缺省值。

 例:

 /_decorator/main.jsp中 (装饰器页面):
 <title>
  <decorator:title default="却省title-hello" />
  - 附加标题
 </title>

 /aaa.jsp中 (原始页面):
 <title>aaa页面</title>

 访问/aaa.jsp的结果:
 <title>aaa页面 - 附加标题</title>

 <decorator:getProperty property="..." [ default="..." ] [
  writeEntireProperty="..." ] />

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

 sitemesh文档中的例子很好理解:The decorator:
 <body bgcolor="white"
  <decorator:getProperty property="body.onload"
   writeEntireProperty="true" />
  >The undecorated page:
  <body onload="document.someform.somefield.focus();">
   The decorated page:
   <body bgcolor="white"
    onload="document.someform.somefield.focus();">

    注意,writeEntireProperty="true"会在插入内容前加入一个空格。

    <decorator:usePage id="..." />
    象jsp页面中的
    <jsp:useBean>
     标签一样,可以使用被包装为一个Page对象的页面。 (懒的用)

     例:可用
     <decorator:usePage id="page" />
     :<%=page.getTitle()%>达到
     <decorator:title />
     的访问结果。

      

     <page:applyDecorator name="..." [ page="..."
      title="..." ]>
      <page:param name="...">...</page:param>
      <page:param name="...">...</page:param>
     </page:applyDecorator>

     应用包装器到指定的页面上,一般用于被包装页面中主动应用包装器。这个标签有点不好理解,我们来看一个例子:

     包装器页面 /_decorators/panel.jsp:
     <p>
      <decorator:title />
     </p>
     ...
     <p>
      <decorator:body />
     </p>
     并且在decorators.xml中有
     <decorator name="panel" page="panel.jsp" />
     一个公共页面,即将被panel包装:/_public/date.jsp: ... <%=new
     java.util.Date()%> ...
     <decorator:getProperty property="myEmail" />
     被包装页面 /page.jsp :
     <title>page的应用</title>
     .....
     <page:applyDecorator name="panel"
      page="/_public/date.jsp">
      <page:param name="myEmail">
       chen_p@neusoft.com
      </page:param>
     </page:applyDecorator>

 

     最后会是什末结果呢?除了/page.jsp会被默认的包装页面包装上header,footer外,page.jsp页面中还内嵌了date.jsp页面,并且此date.jsp页面还会被panel.jsp包装为一个title加body的有2段的页面,第1段是date.jsp的title,第2段是date.jsp的body内容。

 

     另外,page:applyDecorator中包含的page:param标签所声明的属性值还可以在包装页面中用decorator:getProperty标签访问到。


     前面的文章已经足以应用sitemesh来改善您的应用,但我发现还有一些其他的东东可能也会对大家有所帮助
     可打印的界面装饰
     前面说过有1种可打印的装饰器,可以允许你当用http://localhost/aaa/a.html?printable=true方式访问时,应用其他的装饰器(自己指定),给出原始页面以供打印(免得把header,footer等的花哨的图片也搭上)。
     让我们来看一看怎样实现他:

     1.首先在WEB-INFO/sitemesh.xml中设置:
     <mapper
      class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
      <param name="decorator" value="printable" />
      <param name="parameter.name" value="printable" />
      <param name="parameter.value" value="true" />
     </mapper>
     这样就可以通过?printable=true来使用名为printable的装饰器,而不是用原来的装饰器。

     2.在WEB-INFO/decorators.xml中定义相应的printable装饰器
     <decorator name="printable" page="printable.jsp" />

     3.最后编写printable装饰器/decorators/printable.jsp <%@
     taglib uri="sitemesh-decorator" prefix="decorator"
     %>
     <html>
      <head>
       <title>
        <decorator:title />
       </title>
       <decorator:head />
      </head>
      <body>
       <h1>
        <decorator:title />
       </h1>
       <p align="right">
        <i>(printable version)</i>
       </p>
       <decorator:body />
      </body>
     </html>
     这样就可以让一个原始页面通过?printable=true开关来切换不同的装饰器页面。

      
     中文问题由于sitemesh内部所使用的缺省字符集为iso-8859-1,直接使用会产生乱码,我们可以通过以下方法纠正之:
     方法1:可以在您所用的application
     server的配置文件中找一找,有没有设置encoding或charset的项目,然后设成gbk或gb2312即可
     方法2:这也是我们一直使用的方法。1.在每一个jsp页里设置: <%@ page
     contentType="text/html; charset=gbk"%>
     来告诉server你所要求的字符集。2.在每个jsp页的head中定义:
     <META HTTP-EQUIV="content-type"
      CONTENT="text/html; charset=gbk">
      来告诉浏览器你所用的字符集。 总结:使用sitemesh最通常的途径: 1.配置好环境,

      2.在WEB-INFO/decroators.xml中描述你将建立的包装器。

      3.开发在decroators.xml中描述的包装器,最好存放在/_decorators目录下

      4.ok ,可以看看辛勤的成果了 :)

分享到:
评论

相关推荐

    springmvc+mybatis+ehcache+freemarker+sitemesh页面布局(注解)整合实例完美运行

    springmvc+mybatis+ehcache+freemarker+sitemesh页面布局(注解)整合实例完美运行

    JSP布局框架SiteMesh.zip

    SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大 量页面构成的...

    页面装饰器(sitemesh)实例源代码

    用sitemesh页面装饰器,将大名鼎鼎的开源即时通讯服务器openfire中运用的,布局页面抽取出来。MyEclipse中可以直接部署的代码。

    jqueryui界面框架_sitemesh布局框架

    jqueryui界面框架_sitemesh布局框架

    sitemesh-3.0.1-javadoc

    SiteMesh是一个网页布局和装饰框架以及Web应用程序集成框架,可帮助创建由页面组成的网站,这些页面需要一致的外观,导航和布局方案。 SiteMesh会拦截对通过Web服务器请求的任何静态或动态生成的HTML页面的请求,...

    java sitemesh 页面框架

    页面框架 布局,使用方便,已经配置好了,包也在里面

    sitemesh jar包机tld文件

    jsp采用sitemesh页面布局所需的jar包和tld文件

    sitemesh 完美合集 4个资料和jar文件

    SiteMesh是一个Web页面布局修饰框架, 用于构建包含大量页面, 需要一致的外观样式(look/fell), 导航和布局机制的大型网站. sitemesh应用Decorator模式,用filter截取request和response,把页面组件head,content,...

    网络应用程序整合框架SiteMesh源码

    SiteMesh是一款网页布局和装饰器框架,也是一个网络应用程序整合框架,它可以用来维护那些很多页面,并且希望保持所有页面的布局、链接和风格一致的大型网站应用整合与维护。使用SiteMesh可以抽象出页面中的公共布局...

    sitemesh框架学习

    相信大家用过frame页面框架、iframe页面框架、include导入的方式都可以实现框架布局。但是这些方法out了,学习了sitemesh之后你就会发现在页面中的头部和底部自动导入;这才是真技术。像frame、iframe只适合后台框架...

    sitemesh

    SiteMesh是一个Web页面布局修饰框架, 用于构建包含大量页面, 需要一致的外观样式(look/fell), 导航和布局机制的大型网站. SiteMesh应用Decorator模式,用filter截取request和response,把页面组件head,content,...

    SiteMesh v2.4.1.ZIP

    SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。 Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大量页面构成的...

    siteMesh例子

    SiteMesh是一个Web页面布局修饰框架, 用于构建包含大量页面, 需要一致的外观样式(look/fell), 导航和布局机制的大型网站. SiteMesh应用Decorator模式,用filter截取request和response,把页面组件head,content,...

    siteMesh案例

    SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。

    sitemesh(example).rar_sitemesh_装修网站java

    SiteMesh是一个网页的布局和装修的框架和网络应用集成框架,以帮助建立大型网站的网页数量组成为一致的外观/认为,导航和布局计划是必要的。

    sitemesh驱动

    Sitemesh是由一个基于Web页面布局、装饰及与现存Web应用整合的框架。它能帮助我们再由大量页面工程的项目中创建一致的页面布局和外观,如一致的导航条、一致的banner、一致的版权等。

    struts2 +sitemesh:实例学习

    Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大 量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。 它不仅仅能处理动态的...

    SpringBoot框架示例:整合SpringMVC、MyBatis、安全框架Shiro、页面布局框架Sitemesh.zip

    尽管Spring框架自身对编程模型没有限制,但其在Java应用中的频繁使用让它备受青睐,以至于后来让它作为EJB(EnterpriseJavaBeans)模型的补充,甚至是替补。Spring框架为开发提供了一系列的解决方案,比如利用控制...

    SiteMesh.PHP-开源

    古老的Java SiteMesh网页布局和装饰框架到PHP5的端口。

    SITE MESH学习笔记

    SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。

Global site tag (gtag.js) - Google Analytics