★ 作用:sitemesh应用Decorator模式,用filter截取request和response,把页面组件
head,content,banner结合为一个完整的视图[引用]。
★ appfuse的使用:
(1) 在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>
<!—所有的用户请求都要通过sitemesh过滤处理-->
<url-pattern>/*</url-pattern>
<!-- These are needed by Tomcat 5 for forwards -->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
(2)common/taglibs.jsp中的标签申明,只是命名说明,具体调用时,应用会在WEB-INF/lib中的对应的sitemesh-x.x.jar中定位。
<%@ taglib uri="http://www.opensymphony.com/sitemesh/
decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
(3)WEB-INF/sitemesh.xml文件(可选),进行sitemesh配置
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser default="true"
class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>
<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"/>
<!-- Fixes problem with login page not being decorated -->
<!-- https://dudu.dev.java.net/issues/show_bug.cgi?id=63 -->
<parser content-type="text/plain"
class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}"/>
</mapper>
</decorator-mappers>
</sitemesh>
(4)WEB-INF/decorators.xml文件,指定具体的装饰页。
<decorators defaultdir="/decorators">
<excludes>
<pattern>/demos/*</pattern>
<pattern>/resources/*</pattern>
</excludes>
<decorator name="default" page="default.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
(5)装饰页/decorators/default.jsp的主要内容。
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<%@ include file="/common/meta.jsp" %>
<!—把请求的原始页面的title内容插入到<title></title>中间-->
<title><fmt:message key="webapp.prefix"/><decorator:title/></title>
<!—所有被装饰页都会用到的样式表和JavaScript-->
<script type="text/javascript"
src="<c:url value='/scripts/helptip.js'/>"></script>
<link rel="stylesheet" type="text/css" media="all"
href="<c:url value='/styles/default.css'/>" />
<link rel="stylesheet" type="text/css" media="all"
href="<c:url value='/styles/helptip.css'/>" />
<link rel="stylesheet" type="text/css" media="print"
href="<c:url value='/styles/print.css'/>" />
<script type="text/javascript"
src="<c:url value='/scripts/global.js'/>"></script>
<link rel="stylesheet" type="text/css" media="all"
href="<c:url value='/styles/menuExpandable.css'/>" />
<script type="text/javascript"
src="<c:url value='/scripts/menuExpandable.js'/>"></script>
<!--插入原始页面(被包装页面)的head标签中的内容(不包括head标签本身-->
<decorator:head/>
</head>
<!—通过decorator:getProperty在标签处插入原始页面(被包装页面)的原有的标签的属性中的内容,还可以添加一个缺省值-->
<body<decorator:getProperty property="body.id" writeEntireProperty="true"/>>
<div id="screen">
<div id="header">
<c:import url="/common/header.jsp"/>
</div>
<c:import url="/WEB-INF/pages/menu.jsp"/>
<div id="content">
<h1><decorator:getProperty property="page.heading"/></h1>
<%@ include file="/common/messages.jsp" %>
<!--把请求的原始页面的body内的全部内容插入到相应位置-->
<decorator:body/>
</div>
<div id="footer">
<c:import url="/common/footer.jsp"/>
</div>
</div>
</body>
</html>