除了要copy到WEB-INF/lib中的sitemesh.jar外,还有2个文件要建立到WEB-INF/:
并且在decorators.xml中有<decorator name="panel" page="panel.jsp"/>
转自:http://hi.baidu.com/lingyu125/blog/item/b6c31163d0409acde7113aed.html
- 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):
(但一般不用建立它,默认设置足够了: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 : 匹配的路径,可以用*,那些被访问的页面需要被装饰。
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://www.opensymphony.com/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>
<pattern>*</pattern>
</decorator>
这样,请求的所有页面都会被重新处理,并按照main.jsp的格式重新展现在你面前。
以下列着全部标签:
Decorator Tags | Page Tags |
被用于建立装饰器页面. | 被用于从原始内容页面访问装饰器. |
<decorator:head /> <decorator:body /> <decorator:title /> <decorator:getProperty /> <decorator:usePage /> | <page:applyDecorator /> <page:param |
插入原始页面(被包装页面)的head标签中的内容(不包括head标签本身)。
<decorator:body />
插入原始页面(被包装页面)的body标签中的内容。
插入原始页面(被包装页面)的title标签中的内容,还可以添加一个缺省值。
例:
sitemesh文档中的例子很好理解:
The decorator:
The undecorated page:
The decorated page:
The decorator:
<body bgcolor="white"<decorator:getProperty property="body. writeEntireProperty="true" />>
The undecorated page:
<body onload="document.someform.somefield.focus();">
The decorated page:
<body bgcolor="white" onload="document.someform.somefield.focus();">
注意,
writeEntireProperty="true"会在插入内容前加入一个空格。
<page:applyDecorator name="..." [ page="..." title="..." ] >
<page:param name="..."> ... </page:param>
<page:param name="..."> ... </page:param>
</page:applyDecorator>
<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内容。
前面说过有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的装饰器,而不是用原来的装饰器。
<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"/>
<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,直接使用会产生乱码,我们可以通过以下方法纠正之:
|
总结:使用sitemesh最通常的途径: |
1.配置好环境,
2.在WEB-INFO/decroators.xml中描述你将建立的包装器。
3.开发在decroators.xml中描述的包装器,最好存放在/_decorators目录下
|
转自:http://hi.baidu.com/lingyu125/blog/item/b6c31163d0409acde7113aed.html