sitemesh2.4

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

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

好处:

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

部署:

首先需要导入 sitemesh-2.4.2.jar 由于是 struts2.1.8 整合 sitemesh 的所以还需要导入 struts2-sitemesh-plugin-2.1.6.jar( 目前还没有 struts2-sitemesh-plugin-2.1.8.jar 的 ).

Web.xml 文件中的配置:

< filter >

< filter-name > struts2 </ filter-name >

< filter-class > org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </ filter-class >

</ filter >

< filter >

< filter-name > struts-cleanup </ filter-name >

< filter-class > org.apache.struts2.dispatcher.ActionContextCleanUp </ filter-class >

</ filter >

< filter >

< filter-name > sitemesh </ filter-name >

< filter-class > com.opensymphony.module.sitemesh.filter.PageFilter </ filter-class >

</ filter >

< filter-mapping >

< filter-name > struts-cleanup </ filter-name >

< url-pattern > /* </ url-pattern >

< dispatcher > REQUEST </ dispatcher >

< dispatcher > FORWARD </ dispatcher >

</ filter-mapping >

< filter-mapping >

< filter-name > sitemesh </ filter-name >

< url-pattern > /* </ url-pattern >

< dispatcher > REQUEST </ dispatcher >

< dispatcher > FORWARD </ dispatcher >

</ filter-mapping >

< filter-mapping >

< filter-name > struts2 </ filter-name >

< url-pattern > /* </ url-pattern >

< dispatcher > REQUEST </ dispatcher >

< dispatcher > FORWARD </ dispatcher >

</ filter-mapping >

以上的配置是根据 url 进行装饰的。如果想根据物理路径来进行装饰,就把 sitemesh 和 struts2 位置换一下就 ok 了。

Sitemesh 自己也需要配置文件:比如 decorators.xml , sitemesh.xml , sitemesh-decorator.tld , sitemesh-page.tld 。 decorators.xml 是必须要的,其它的可要可不要,主要取决于项目的复杂性。

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 >

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

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

sitemesh 标签讲解:

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

SiteMesh基本用法及示例

SiteMesh是基于Servlet的filter的,即过滤流。它是通过截取reponse,并进行装饰后再交付给客户。

其中涉及到两个名词: 装饰页面(decorator page)和 “被装饰页面(Content page)" , 即 SiteMesh通过对Content Page的装饰,最终得到页面布局和外观一直的页面,

并返回给客户

运行环境需要:servlet2.3 , JDK1.4 以上。

正常模式下的web访问流程

加入SiteMesh装饰的web访问流程

一:搭建SiteMesh环境及简单使用

1.1:准备资源

siteMesh2.4.jar, sitemesh-page.tld , sitemesh-decorator.tld 这个三个必要文件

将jar包复制进/WEB-INF/lib目录下, 两个tld文件导入/WEB-INF下即可

在web.xml中加入siteMesh的filter和taglib

[html] view plain copy print ?
  1. <filter>
  2. <filter-name>sitemesh</filter-name>
  3. <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>sitemesh</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>
  9. <!-- not required for containers that fully support JSP 1.2 -->
  10. <taglib>
  11. <taglib-uri>sitemesh-page</taglib-uri>
  12. <taglib-location>/WEB-INF/lib/sitemesh-page.tld</taglib-location>
  13. </taglib>
  14. <taglib>
  15. <taglib-uri>sitemesh-decorator</taglib-uri>
  16. <taglib-location>/WEB-INF/lib/sitemesh-decorator.tld</taglib-location>
  17. </taglib>
 <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>
<!-- not required for containers that fully support JSP 1.2 -->
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>/WEB-INF/lib/sitemesh-page.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>/WEB-INF/lib/sitemesh-decorator.tld</taglib-location>
</taglib>

1.2 建立decorators.xml

在/WEB-INF下创建decorators.xml文件,siteMesh通过该文件来获知"装饰页面"和"被装饰页面"的映射

decorators.xml

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 默认目录 -->
  3. <decorators defaultdir="/decorators">
  4. <!-- 缺省装饰页 -->
  5. <decorator name="main" page="main.jsp">
  6. <pattern>/*</pattern>
  7. </decorator>
  8. <!-- 自定义装饰页,我们下面实例就是这部分起作用 -->
  9. <decorator name="mai" page="mai.jsp">
  10. <pattern>/mai.html</pattern>
  11. </decorator>
  12. <!-- 只装饰一个页面也可用这种方式定义 -->
  13. <decorator name="panel" page="panel.jsp"/>
  14. <!-- 装饰velocity模板 -->
  15. <decorator name="velocity" page="velocity.vm">
  16. <pattern>/velocity.html</pattern>
  17. </decorator>
  18. <!-- 装饰freeMarker模板 -->
  19. <decorator name="freemarker" page="freemarker.ftl">
  20. <pattern>/freemarker.html</pattern>
  21. </decorator>
  22. <decorator name="test" page="test.jsp">
  23. <pattern>/agent.jsp</pattern>
  24. </decorator>
  25. </decorators>
<?xml version="1.0" encoding="UTF-8"?>
<!-- 默认目录 -->
<decorators defaultdir="/decorators">
<!-- 缺省装饰页 -->
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
<!-- 自定义装饰页,我们下面实例就是这部分起作用 -->
<decorator name="mai" page="mai.jsp">
<pattern>/mai.html</pattern>
</decorator>
<!-- 只装饰一个页面也可用这种方式定义 -->
<decorator name="panel" page="panel.jsp"/>
<!-- 装饰velocity模板 -->
<decorator name="velocity" page="velocity.vm">
<pattern>/velocity.html</pattern>
</decorator>
<!-- 装饰freeMarker模板 -->
<decorator name="freemarker" page="freemarker.ftl">
<pattern>/freemarker.html</pattern>
</decorator>
<decorator name="test" page="test.jsp">
<pattern>/agent.jsp</pattern>
</decorator>
</decorators>

下边是对上边中所缺少的一些补充

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <decorators defaultdir="/decorators">
  3. <!-- 此处用来定义不需要过滤的页面 -->
  4. <excludes>
  5. </excludes>
  6. <!-- 用来定义装饰器要过滤的页面 -->
  7. <decorator name="main" page="main.jsp">
  8. <pattern>/*</pattern>
  9. </decorator>
  10. </decorators>
<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
<!-- 此处用来定义不需要过滤的页面 -->
<excludes>
</excludes>
<!-- 用来定义装饰器要过滤的页面 -->
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>  

1.3 装饰页的创建

在web目录(或者webContent)下创建文件夹decorators,在文件夹中建立mai.jsp文件

[html] view plain copy print ?
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. /*这里导入了SiteMesh的标签库 */
  6. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
  7. <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  10. OK,there is a decorator begin!<hr />
  11. /*这里的意思是,被装饰页的title内容将会在这里插入 */
  12. <decorator:title></decorator:title>
  13. </head>
  14. <body>
  15. /*被修饰页的body内容将在这里插入
  16. <decorator:body></decorator:body>
  17. <hr />Yse,there is a decorator end !
  18. </body>
  19. </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
/*这里导入了SiteMesh的标签库 */
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
OK,there is a decorator begin!<hr />
/*这里的意思是,被装饰页的title内容将会在这里插入 */
<decorator:title></decorator:title>
</head>
<body>
/*被修饰页的body内容将在这里插入
<decorator:body></decorator:body>
<hr />Yse,there is a decorator end !
</body>
</html>

1.4 被修饰页的创建

在web目录(或webContent)下创建mai.html

[html] view plain copy print ?
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. this is the Content Page !!!
  9. </body>
  10. </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
this is the Content Page !!!
</body>
</html>

1.5 使用tomcat进行示例运行,访问http://localhost:8080/{your project name}/mai.html , 运行结果如下:

1.6 sitemesh.xml的配置(可选, 示例中没有用到该文件)

该配置文件用于高级元素的配置,有具体需要的可以配置

[html] view plain copy print ?
  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" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
  6. </page-parsers>
  7. <decorator-mappers>
  8. <mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
  9. <param name="property.1" value="meta.decorator" />
  10. <param name="property.2" value="decorator" />
  11. </mapper>
  12. <mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
  13. </mapper>
  14. <mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
  15. <param name="match.MSIE" value="ie" />
  16. <param name="match.Mozilla [" value="ns" />
  17. <param name="match.Opera" value="opera" />
  18. <param name="match.Lynx" value="lynx" />
  19. </mapper>
  20. <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
  21. <param name="decorator" value="printable" />
  22. <param name="parameter.name" value="printable" />
  23. <param name="parameter.value" value="true" />
  24. </mapper>
  25. <mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
  26. <param name="decorator" value="robot" />
  27. </mapper>
  28. <mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
  29. <param name="decorator.parameter" value="decorator" />
  30. <param name="parameter.name" value="confirm" />
  31. <param name="parameter.value" value="true" />
  32. </mapper>
  33. <mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
  34. </mapper>
  35. <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
  36. <param name="config" value="${decorators-file}" />
  37. </mapper>
  38. </decorator-mappers>
  39. </sitemesh>
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" />
<param name="property.2" value="decorator" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
<param name="match.MSIE" value="ie" />
<param name="match.Mozilla [" value="ns" />
<param name="match.Opera" value="opera" />
<param name="match.Lynx" value="lynx" />
</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>
<mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
<param name="decorator" value="robot" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
<param name="decorator.parameter" value="decorator" />
<param name="parameter.name" value="confirm" />
<param name="parameter.value" value="true" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>

使用总结:整个过程配置是相对简单的,先导入所需资源,然后再配置filter,之后是derator page和content page的创建以及他们之间的映射关系,配置命令是相对简单的,简单的需求上面这些已经足矣。

二:使用示例

2.1 例子1

在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator

[html] view plain copy print ?
  1. <decorator name="mydecorator1" page="mydecorator1.jsp">
  2. <pattern>/test1.jsp</pattern>
  3. </decorator>
<decorator name="mydecorator1" page="mydecorator1.jsp">
<pattern>/test1.jsp</pattern>
</decorator>

在{myapp}/decorators目录下添加mydecorator1.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
  2. <html>
  3. <head>
  4. <title>My Site - <decorator:title default="Welcome!" /></title>
  5. <decorator:head />
  6. </head>
  7. <body>
  8. <decorator:body />
  9. <p>This message is in /decorators/mydecorator1.jsp</p>
  10. </body>
  11. </html>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
<head>
<title>My Site - <decorator:title default="Welcome!" /></title>
<decorator:head />
</head>
<body>
<decorator:body />
<p>This message is in /decorators/mydecorator1.jsp</p>
</body>
</html>

在{myapp}目录下添加test1.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>This is test1</title>
  7. </head>
  8. <body>
  9. <b>This is test1</b>
  10. </body>
  11. </html>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is test1</title>
</head>
<body>
<b>This is test1</b>
</body>
</html>
  • 打开浏览器,访问http://localhost:8080/myapp/test1.jsp,将会出现一下内容:

This is test1

This message is in /decorators/mydecorator1.jsp

2.2 例子2(decorator:getProperty)

有时候,我们期望修改页面中某个有固定标记的片段,例如我们的jsp中有一个标记<mytag>...</mytag>,此时可以用如下方法实现:

在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator

[html] view plain copy print ?
  1. <decorator name="mydecorator2" page="mydecorator2.jsp">
  2. <pattern>/test2.jsp</pattern>
  3. </decorator>
<decorator name="mydecorator2" page="mydecorator2.jsp">
<pattern>/test2.jsp</pattern>
</decorator>

在{myapp}/decorators目录下添加mydecorator2.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
  2. <html>
  3. <head>
  4. <title>My Site - <decorator:title default="Welcome!" /></title>
  5. <decorator:head />
  6. </head>
  7. <body>
  8. <decorator:body />
  9. <decorator:getProperty property="page.content1"/>
  10. <decorator:getProperty property="page.content2"/>
  11. <!-- do nothing -->
  12. <decorator:getProperty property="page.content3"/>
  13. <p>This message is in /decorators/mydecorator2.jsp</p>
  14. </body>
  15. </html>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
<head>
<title>My Site - <decorator:title default="Welcome!" /></title>
<decorator:head />
</head>
<body>
<decorator:body />
<decorator:getProperty property="page.content1"/>
<decorator:getProperty property="page.content2"/>
<!-- do nothing -->
<decorator:getProperty property="page.content3"/>
<p>This message is in /decorators/mydecorator2.jsp</p>
</body>
</html>

在{myapp}目录下添加test2.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>This is test2</title>
  7. </head>
  8. <body>
  9. <b>This is test2</b>
  10. <b>Use <decorator:getProperty> tag</b>
  11. <content tag="content1"><p>This is content1</p></content>
  12. <content tag="content2"><p>This is content2</p></content>
  13. <content tag="content4"><p>This is content4, it shouldn't be display</p></content>
  14. </body>
  15. </html>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is test2</title>
</head>
<body>
<b>This is test2</b>
<b>Use <decorator:getProperty> tag</b>
<content tag="content1"><p>This is content1</p></content>
<content tag="content2"><p>This is content2</p></content>
<content tag="content4"><p>This is content4, it shouldn't be display</p></content>
</body>
</html>

打开浏览器,访问http://localhost:8080/myapp/test2.jsp,将会出现一下内容:

This is test2

Use <decorator:getProperty> tag

This is content1

This is content2

This message is in /decorators/mydecorator2.jsp

2.3 例子3 (page:applyDecorator tag)

在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator:

[html] view plain copy print ?
  1. <decorator name="mydecorator3" page="mydecorator3.jsp">
  2. <pattern>/test3.jsp</pattern>
  3. </decorator>
  4. <decorator name="mydecorator31" page="mydecorator31.jsp">
  5. </decorator>
<decorator name="mydecorator3" page="mydecorator3.jsp">
<pattern>/test3.jsp</pattern>
</decorator>
<decorator name="mydecorator31" page="mydecorator31.jsp">
</decorator>

在{myapp}/decorators目录下添加mydecorator3.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
  2. <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
  3. <html>
  4. <head>
  5. <title>My Site - <decorator:title default="Welcome!" /></title>
  6. <decorator:head />
  7. </head>
  8. <body>
  9. <decorator:body />
  10. <page:applyDecorator name="mydecorator31">
  11. <content tag="content1"><p>This is content1</p></content>
  12. <content tag="content2"><p>This is content2</p></content>
  13. </page:applyDecorator>
  14. </body>
  15. </html>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<html>
<head>
<title>My Site - <decorator:title default="Welcome!" /></title>
<decorator:head />
</head>
<body>
<decorator:body />
<page:applyDecorator name="mydecorator31">
<content tag="content1"><p>This is content1</p></content>
<content tag="content2"><p>This is content2</p></content>
</page:applyDecorator>
</body>
</html>

在{myapp}/decorators目录下添加mydecorator31.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
  2. <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
  3. <p><i>begin</i></>
  4. <decorator:getProperty property="page.content1"/>
  5. <decorator:getProperty property="page.content2"/>
  6. <p><i>end</i></>
  7. 在{myapp}目录下添加test3.jsp文件,内容如下:
  8. <%@page contentType="text/html"%>
  9. <%@page pageEncoding="UTF-8"%>
  10. <html>
  11. <head>
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  13. <title>This is test3</title>
  14. </head>
  15. <body>
  16. <b>This is test3</b>
  17. <b>Use <page:applyDecorator> tag</b>
  18. </body>
  19. </html>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<p><i>begin</i></>
<decorator:getProperty property="page.content1"/>
<decorator:getProperty property="page.content2"/>
<p><i>end</i></>
在{myapp}目录下添加test3.jsp文件,内容如下:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is test3</title>
</head>
<body>
<b>This is test3</b>
<b>Use <page:applyDecorator> tag</b>
</body>
</html>

注意:相对于例子2,这里已经没有了<content tag="XXX"/>标签。

打开浏览器,访问http://localhost:8080/myapp/test3.jsp,将会出现一下内容:

This is test3

Use <page:applyDecorator> tag

begin

This is content1

This is content2

end

这里,我在mydecorator3.jsp中应用了mydecorator31.jsp的的decorator,并且将原来在test2.jsp中的 <content />标签复制到mydecorator3.jsp中,此时对于<content tag="xxx"/>的标签将会由mydecorator31.jsp了装饰。

2.4 例子4(page:parm tag)

在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator:

[html] view plain copy print ?
  1. <decorator name="mydecorator4" page="mydecorator4.jsp">
  2. <pattern>/test4.jsp</pattern>
  3. </decorator>
  4. <decorator name="mydecorator41" page="mydecorator41.jsp">
  5. </decorator>
<decorator name="mydecorator4" page="mydecorator4.jsp">
<pattern>/test4.jsp</pattern>
</decorator>
<decorator name="mydecorator41" page="mydecorator41.jsp">
</decorator>

在{myapp}/decorators目录下添加mydecorator4.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
  2. <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
  3. <html>
  4. <head>
  5. <title>My Site - <decorator:title default="Welcome!" /></title>
  6. <decorator:head />
  7. </head>
  8. <body>
  9. <decorator:body />
  10. <page:applyDecorator name="mydecorator41" >
  11. <content tag="content1"><p>This is content1</p></content>
  12. <content tag="content2"><p>This is content2</p></content>
  13. <page:param name="page.content1"><p>This content1 has been replaced</p></page:param>
  14. </page:applyDecorator>
  15. </body>
  16. </html>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<html>
<head>
<title>My Site - <decorator:title default="Welcome!" /></title>
<decorator:head />
</head>
<body>
<decorator:body />
<page:applyDecorator name="mydecorator41" >
<content tag="content1"><p>This is content1</p></content>
<content tag="content2"><p>This is content2</p></content>
<page:param name="page.content1"><p>This content1 has been replaced</p></page:param>
</page:applyDecorator>
</body>
</html>

在{myapp}/decorators目录下添加mydecorator41.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
  2. <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
  3. <p><i>begin</i></>
  4. <decorator:getProperty property="page.content1"/>
  5. <decorator:getProperty property="page.content2"/>
  6. <p><i>end</i></>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<p><i>begin</i></>
<decorator:getProperty property="page.content1"/>
<decorator:getProperty property="page.content2"/>
<p><i>end</i></>

在{myapp}目录下添加test4.jsp文件,内容如下:

[html] view plain copy print ?
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>This is test4</title>
  7. </head>
  8. <body>
  9. <b>This is test4</b>
  10. <b>Use <page:param> tag</b>
  11. </body>
  12. </html>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is test4</title>
</head>
<body>
<b>This is test4</b>
<b>Use <page:param> tag</b>
</body>
</html> 

打开浏览器,访问http://localhost:8080/myapp/test4.jsp,将会出现一下内容:

This is test4

Use <page:param> tag

begin

This content1 has been replaced

This is content2

end

这里,我在mydecorator4.jsp中应用了mydecorator41.jsp的的decorator,并且添加了<page:param name="page.content1">标签,那么此时页面上将会用<page:param>标签中的内容替换原来在<decorator:getProperty property="page.content1"/>中的内容,因此页面将不在This is content1”而显示“This content1 has been replaced

SiteMesh的一个重要特性是使用原始HTML的meta标签(例如<meta name="foo" content="bar">)从基础页面传递信息到装饰器。作为一个例子,下面我们使用一个meta标签来定义HTML页面的作者。

[html] view plain copy print ?
  1. < html >
  2. < meta name = " author "  content = " test@example.com " >
  3. < head >
  4. < title > Simple Document </ title >
  5. </ head >
  6. < body >
  7. Hello World !   < br  />
  8. <%=   1 + 1   %>
  9. </ body >
  10. </ html >
< html >
< meta name = " author "  content = " test@example.com " >
< head >
< title > Simple Document </ title >
</ head >
< body >
Hello World !   < br  />
<%=   1 + 1   %>
</ body >
</ html >  

我们定义一个“smart”装饰器来研究meta标签,如果出现这个标签,则可以得到一个相应的HTML:

[html] view plain copy print ?
  1. <% @ taglib uri = " sitemesh-decorator "  prefix = " decorator "   %>
  2. < decorator:usePage id = " myPage "   />
  3. < html >
  4. < head >
  5. < title >
  6. My Site  -   < decorator:title  default = " Welcome! "   />
  7. </ title >
  8. < decorator:head  />
  9. </ head >
  10. < body >
  11. < h1 >< decorator:title  default = " Welcome! "   /></ h1 >
  12. < h3 >
  13. < a href = " mailto: <decorator:getProperty property= " meta.author "   default= " staff@example.com "  /> " >
  14. < decorator:getProperty property = " meta.author "   default = " staff@example.com "   />
  15. </ a >
  16. </ h3 >
  17. < hr  />
  18. < decorator:body  />
  19. < p >
  20. < small >   ( < a href = " /?printable=true " > printable version </ a > )   </ small >
  21. </ p >
  22. </ body >
  23. </ html >
<% @ taglib uri = " sitemesh-decorator "  prefix = " decorator "   %>
< decorator:usePage id = " myPage "   />
< html >
< head >
< title >
My Site  -   < decorator:title  default = " Welcome! "   />
</ title >
< decorator:head  />
</ head >
< body >
< h1 >< decorator:title  default = " Welcome! "   /></ h1 >
< h3 >
< a href = " mailto: <decorator:getProperty property= " meta.author "   default= " staff@example.com "  /> " >
< decorator:getProperty property = " meta.author "   default = " staff@example.com "   />
</ a >
</ h3 >
< hr  />
< decorator:body  />
< p >
< small >   ( < a href = " /?printable=true " > printable version </ a > )   </ small >
</ p >
</ body >
</ html >  

可以看到我们使用了 getProperty标签的 一个默认属性——如果没有指定author,我们就设定其为staff。如果你决定使用这个模型储存页面的meta数据,你或许需要和你的开发伙伴一起来 确定将使用什么标签以及如何使用他们。简单的,你或许想要使用meta标签来描述诸如页面作者及时间戳之类的东西。更复杂一些,你或许会想像XML文件一 样标准化的管理你的站点导航,同时使用meta标签来通过页面节点转到装饰器。

sitemesh 配置

使用的sitemesh包是sitemesh-2.4.jar现将这个包加入到WEB-INF/lib下也可以加入sitemesh-page.tld , sitemesh-decorator.tld两个文件(没有也没有影响)然后在web.xml下配置拦截器代码如下

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <display-name></display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <filter>
  11. <filter-name>sitemesh</filter-name>
  12. <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>sitemesh</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18. </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>

定义一个decorators.xml 放在WEB-INF根目录下,配置内容提供以下示例

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <decorators defaultdir="/decorators">
  3. <decorator name="mai" page="mai.jsp">
  4. <pattern>/mai.html</pattern>
  5. </decorator>
  6. </decorators>
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<decorator name="mai" page="mai.jsp">
<pattern>/mai.html</pattern>
</decorator>
</decorators>

在WebRoot下面新建一个decorators文件夹 然后将装饰页面放在该目录如图所示

mai.jsp.内容如下所示

[html] view plain copy print ?
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
  2. <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
  3. <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>My JSP 'mai.jsp' starting page</title>
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. <!--
  19. <link rel="stylesheet" type="text/css" href="styles.css">
  20. -->
  21. <decorator:title></decorator:title>
  22. </head>
  23. <body>
  24. there is a decorator begin!<hr />
  25. <decorator:body></decorator:body>
  26. hello world <hr/>
  27. </body>
  28. </html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'mai.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<decorator:title></decorator:title>
</head>
<body>
there is a decorator begin!<hr />
<decorator:body></decorator:body>
hello world <hr/>
</body>
</html>

引入了两个标签库<decorator:title></decorator:title>用来将被装饰页面的title放在此处。<decorator:body></decorator:body>用来将被装饰页面的body内容放在此处。

在WebRoot根目录下面新建一个mai.html  示例内容如下

[html] view plain copy print ?
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>maqiang</title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  9. </head>
  10. <body>
  11. This is my HTML page. <br>
  12. </body>
  13. </html>
<!DOCTYPE html>
<html>
<head>
<title>maqiang</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
This is my HTML page. <br>
</body>
</html>

这样就ok了

sitemesh 了解相关推荐

  1. SiteMesh介绍

    1. SiteMesh简介 SiteMesh是由一个基于Web页面布局.装饰以及与现存Web应用整合的框架.它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的bann ...

  2. Struts和Sitemesh整合,实现多个装饰器

    2019独角兽企业重金招聘Python工程师标准>>> web.xml配置 <filter><filter-name>struts-prepare</f ...

  3. sitemesh排除装饰action的心得

    sitemesh装饰模式的强大就不用说了,但使用过程中遇到的最郁闷的莫过于排除装饰action的困扰了,例如使用某个ajax请求要求返回不装饰的数据,但sitemesh却死活给你装饰了,以前用了个小技 ...

  4. Sitemesh排除Exclude不装饰特定页面的解决办法

    有时候项目中有些文件不需要Sitemesh装饰,例如Error.htm, Error.jsp, OnlineHelp.htm等等.但是用Sitemesh的Exclude不管用,用Printable也不 ...

  5. sitemesh的使用

    由于最近项目的原因,接触到了sitemesh. SiteMesh是一个用来在JSP中实现页面布局和装饰(layout and decoration)的框架组件,能够帮助网站开发人员较容易实现页面中动态 ...

  6. sitemesh官网简介,安装配置教程。(非常适合新手)

    新手一枚,最近研究openfire,看到网上资料其页面布局用的是sitemesh,第一次听说,上网查了好多资料,也看了好多博客介绍,觉得好神奇的技术(高手勿喷).不过我也很好奇现在和sitemesh类 ...

  7. SiteMesh:一个优于Apache Tiles的Web页面布局、装饰框架

    一.SiteMesh项目简介 OS(OpenSymphony)的SiteMesh是一个用来在JSP中实现页面布局和装饰(layout and decoration) 的框架组件,能够帮助网站开发人员较 ...

  8. struts2.2.1关于sitemesh和freemarker整合的细节

    http://nighty.iteye.com/blog/788241 有段时间没有使用struts2了,上了官网发现已经更新到2.2.1版本,没有细看note,照着以前老的方式配置web.xml,发 ...

  9. Struts2整合SiteMesh

    http://takeme.iteye.com/blog/1716488 1.导入Struts2的jar 和 sitemesh.jar 和 Struts2-sitemesh-plugin.jar  c ...

  10. 使用sitemesh建立复合视图 - 1.hello

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

最新文章

  1. 从头开始学习Adobe Photoshop CC图像编辑
  2. MySql中的varchar类型
  3. 我一个女孩子居然做了十年硬件。​。。
  4. Struts Gossip: 模組化程式
  5. 一阶电路暂态响应的结果分析。_阻尼比测试方法及谐响应分析
  6. c语言中case1 case3 n =1,(n 1)-Step Derivations on n-Groupoids: The Case n = 3
  7. ashx在web.config中如何配置_如何在 Istio 1.6 中配置 Prometheus-Operator 和抓取指标
  8. 强大的漏洞扫描工具--nessus
  9. Android 学习笔记 databinding简单使用:使用databinding在listview加入不同类型的view
  10. 苹果:两个 M1 Max 拼一块儿,“史上最强 PC 芯片”M1 Ultra 就出来了!
  11. 水很深的深度学习-Task02机器学习基础
  12. 【堆排序的递归和非递归实现】Java实现
  13. Leetcode|DFS|130. 被围绕的区域
  14. 深海迷航代码_?《深海迷航(Subnautica)》如何输入代码
  15. 能让你「情商暴涨」的6个聊天小技巧
  16. H3C三层交换机之IRF虚拟化技术详解及配置
  17. win10下OpenJtag驱动安装
  18. ArcGIS中的 .tpk数据
  19. Java如何实现浅克隆与深克隆_Java浅谈克隆clone
  20. 我的一百个2019(五):2019,我为什么还在坚持?

热门文章

  1. 项目管理知识领域--十五至尊图
  2. 比亚迪汽车的优势是什么
  3. 苏宁11.11:如何 hold 住大促红包
  4. grx1660linux看视频掉帧,游戏卡顿疯狂掉帧?买它们重获流畅体验
  5. 小试牛刀 最新版微信2.7.1.88 通过CE读取微信登陆用户信息用C++读取出来
  6. mysql存入订单号不重复_MSSQL高并发下生成连续不重复的订单号
  7. Photoshop-图像的排版合并以及优化
  8. [CTO札记]PPT技巧:产品规划应‘图穷才匕现’
  9. js中字符串转成对象,对象转成字符串
  10. 叹:::::再强大的影视神作也会有人诋毁