sitemesh主要有三个主要文件:sitemesh.xml,decorators.xml和做布局用的jsp页面。

一、sitemesh.xml

对于sitemesh.xml这个文件,官方文档上说它不是必须的。sitemesh总是会有一个份默认的配制。如果你打算提供自己的sitemesh.xml,也最好放在web-inf目录下,目前我还没有找到该文件的位置是否可配制以及在何处配制的说明。从sitemesh.xml文件内容上看它主要负责配制3方面的信息:1.装饰器描述文件decorators.xml的存放位置和名称,默认是/WEB-INF/decorators.xml。2.page-parsers的配制。对这一项的不配制目前还是很清楚,大概是指明解析页面使用的类吧。其中字符集的设定可能需要注意,否则可能会出现乱码问题吧?个人猜测。3.decorator-mappers,目前尚不清楚用途。下面是官方提供的一份配制样本.

[xhtml] view
plain
copy

  1. <sitemesh>
  2. <property name="decorators-file" value="/WEB-INF/decorators.xml" />
  3. <excludes file="${decorators-file}" />
  4. <page-parsers>
  5. <parser content-type="text/html"
  6. class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
  7. <parser content-type="text/html;charset=ISO-8859-1"
  8. class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
  9. </page-parsers>
  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>

二、decorators.xml

decorators.xml是配制的核心,它主要用来描述一系列的decorator.每一个decorator需要指明实际用来装饰的JSP页面是哪一个(在这个页面中往往会出现<decorator:head /><decorator:body /><decorator:title /><decorator:getProperty /><decorator:usePage />这些装饰标签),以及这个decorator要装饰的页面有哪些(也就是一系列的url pattern)。下面是oobbs系统的decorators.xml.在它里有两个decorator.分别对应该系统的用户页面和管理页面。需要特别说明的是defaultdir是用来指明装饰文件所在的默认文件夹,如果指定了这个文件夹,那么<decorator/>标签中的page属性中如果填入相对路径,它将会从defaultdir开始查找。在下面的例子中,由于page都给出的是绝对路径,因此defaultdir并不起作用。

[xhtml] view
plain
copy

  1. <decorators defaultdir="/decorators">
  2. <excludes>
  3. <pattern>/40*.jsp</pattern>
  4. <pattern>/*ajax=true*</pattern>
  5. <pattern>/scripts/dojo/*</pattern>
  6. <pattern>/struts/dojo/*</pattern>
  7. <pattern>/resources/*</pattern>
  8. <pattern>/admin/login.jsp</pattern>
  9. <pattern>/site/login.jsp</pattern>
  10. </excludes>
  11. <decorator name="adminDecorator" page="/admin/decorator.jsp">
  12. <pattern>/admin/*</pattern>
  13. </decorator>
  14. <decorator name="siteDecorator" page="/site/decorator.jsp">
  15. <pattern>/site/*</pattern>
  16. </decorator>
  17. </decorators>

三.做布局用的jsp页面

decorators.xml的配制中都会指向一些这样的页面。这些页面是一些普通的jsp页面,主要是用来布局的。像header,footer这样的框架部分用<jsp:include page="xxx.jsp" />引入,需要被装饰的部分用decorator标签替换就可以了。下面是一个样本。其中<decorator:body/>就代表被装饰页面的body部分,在生成html时,这部分会被sitemesh动态替换掉。

[xhtml] view
plain
copy

  1. <%@ include file="/admin/taglibs.jsp"%>
  2. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"  prefix="decorator"%>
  3. <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title><decorator:title/></title>
  9. <link rel="stylesheet" type="text/css" href="<c:url value='/admin/styles/layout.css'/>" />
  10. </head>
  11. <body>
  12. <div id="container">
  13. <div id="header">
  14. <jsp:include page="header.jsp" />
  15. </div>
  16. <div id="menu">
  17. <jsp:include page="menu.jsp" />
  18. </div>
  19. <div id="mainContent">
  20. <div id="sidebar">
  21. <jsp:include page="sidebar.jsp" />
  22. </div>
  23. <div id="content">
  24. <decorator:body/>
  25. </div>
  26. </div>
  27. <div id="footer">
  28. <jsp:include page="footer.jsp" />
  29. </div>
  30. </div>
  31. </body>
  32. </html>

最新文章

  1. word20161220
  2. 一个漂亮的上传按钮input[type=file]
  3. 我的iOS之路2
  4. Storm系列(二):使用Csharp创建你的第一个Storm拓扑(wordcount)
  5. 关于Kendo UI的使用心得
  6. linux lsmod命令 及相关信息
  7. EntityFramework 更新表结构到数据库
  8. [Unity菜鸟] 产生各不相同的随机数
  9. linux有用技巧:使用快照制作虚拟机
  10. org.hibernate.exception.JDBCConnectionException: could not execute query
  11. C# 在RichTextBox中滚动鼠标时滚动的是父窗口的滚动条
  12. C语言--总结报告
  13. php curl请求
  14. 整理4种Vue组件通信方式
  15. 转HDMI
  16. DOIS 2019 DevOps国际峰会北京站来袭~
  17. 通过 ppa 在ubuntu server 上安装java 8
  18. freemark、jsp&amp;css
  19. CF161D Distance in Tree(点分治)
  20. Appium 点击屏幕

热门文章

  1. new、delete用法(一)
  2. Object layout in C++ and access control
  3. Gtest打桩函数
  4. 彻底弄清c标准库中string.h里的常用函数用法
  5. Django 基础
  6. 关于if/else if
  7. c语言exit和return区别,在fork和vfork中使用
  8. 使用expect实现批量操作的自动化
  9. 【HDOJ】2255 奔小康赚大钱
  10. 移动大数据时代最IN编程语言必读书单