参考:http://limuquan.iteye.com/blog/251521
<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"会在插入内容前加入一个空格。
中文API:http://docs.huihoo.com/java/sitemesh/2.html#decorator:getProperty
SIteMesh官方地址:http://www.opensymphony.com/sitemesh/index.html
SiteMesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。 它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,如htm的内容,使得它的内容也符合你的页面结构的要求。甚至于它能将HTML文件象include那样将该文件作为一个面板的形式嵌入到别的文件中去。所有的这些,都是GOF的Decorator模式的最生动的实现。尽管它是由java语言来实现的,但它能与其他Web应用很好地集成
wiki的配图:
http://wiki.sitemesh.org/download/attachments/294923/Figure+1+with+Mobile.png?version=8&modificationDate=1342584619410
|
接下来通过一个SiteMesh简单例子来了解SiteMesh的功能:
- 将sitemesh-2.3.jar放 到 [web-app]/WEB-INF/lib目录下;
- 在[web-app]/WEB-INF/新建一个decorators.xml文件,包含以下内容:
- <?xml version="1.0" encoding="utf-8"?>
- <decorators defaultdir="/decorators">
- <!-- 此处用来定义不需要过滤的页面 -->
- <excludes>
- </excludes>
- <!-- 用来定义装饰器要过滤的页面 -->
- <decorator name="main" page="main.jsp">
- <pattern>/*</pattern>
- </decorator>
- </decorators>
- <?xml version="1.0" encoding="utf-8"?>
- <decorators defaultdir="/decorators">
- <!-- 此处用来定义不需要过滤的页面 -->
- <excludes>
- </excludes>
- <!-- 用来定义装饰器要过滤的页面 -->
- <decorator name="main" page="main.jsp">
- <pattern>/*</pattern>
- </decorator>
- </decorators>
- 在[web-app]/WEB-INF/web.xml添加以下内容:
- <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>
- <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>
- 在[web-app]下创建一个decorators文件夹,在该文件下再创建一个装饰页面main.jsp,包含以下内容:
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- 第一个装饰页面 -->
- <head>
- <!-- 从被装饰页面获取title标签内容,并设置默认值-->
- <title><decorator:title default="默认title"/></title>
- <!-- 从被装饰页面获取head标签内容 -->
- <decorator:head/>
- </head>
- <body>
- <h2>SiteMesh装饰header</h2>
- <hr />
- <!-- 从被装饰页面获取body标签内容 -->
- <decorator:body />
- <hr />
- <h2>SiteMesh装饰footer</h2>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- 第一个装饰页面 -->
- <head>
- <!-- 从被装饰页面获取title标签内容,并设置默认值-->
- <title><decorator:title default="默认title"/></title>
- <!-- 从被装饰页面获取head标签内容 -->
- <decorator:head/>
- </head>
- <body>
- <h2>SiteMesh装饰header</h2>
- <hr />
- <!-- 从被装饰页面获取body标签内容 -->
- <decorator:body />
- <hr />
- <h2>SiteMesh装饰footer</h2>
- </body>
- </html>
- 在[web-app]下创建被装饰页面index.jsp,包含以下内容:
- <%@ 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>
- <!-- 第一个被装饰(目标)页面 -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>被装饰(目标)页面title</title>
- </head>
- <body>
- <h4>被装饰(目标)页面body标签内内容。</h4>
- <h3>使用SiteMesh的好处?</h3>
- <ul>
- <li>被装饰(目标)页面和装饰页面完全分离。</li>
- <li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>
- <li>更容易实现统一的网站风格。</li>
- <li>还有。。。</li>
- </ul>
- </body>
- </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>
- <!-- 第一个被装饰(目标)页面 -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>被装饰(目标)页面title</title>
- </head>
- <body>
- <h4>被装饰(目标)页面body标签内内容。</h4>
- <h3>使用SiteMesh的好处?</h3>
- <ul>
- <li>被装饰(目标)页面和装饰页面完全分离。</li>
- <li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>
- <li>更容易实现统一的网站风格。</li>
- <li>还有。。。</li>
- </ul>
- </body>
- </html>