頁面裝飾器框架-SiteMesh應用Decorator模式
通過SiteMesh的頁面裝飾,可以提高更好的程式碼複用,以下是配置使用的簡單記錄:
1.下載jar包:sitemesh-2.3.jar,將jar複製到lib目錄下
2.web.xml檔案中配置sitemesh的Filter
<!-- 配置裝飾器filter -->
<filter>
<filter-name>sitemeshFilter</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemeshFilter</filter-name>
<url-pattern>/jsp/*</url-pattern>
</filter-mapping>
3.新建一個decorators.xml檔案:
<decorators defaultdir="/WEB-INF/jsp">
<!-- 配置裝飾器頁面-->
<decorator name="decorator" page="/jsp/decorator.jsp" >
<!-- 配置需要追加裝飾器的頁面 -->
<pattern>/jsp/*</pattern>
</decorator>
<!-- 配置不需要追加裝飾的頁面 -->
<excludes>
<pattern>/jsp/decorator*</pattern>
</excludes>
</decorators>
4.新建裝飾頁面decorator.jsp
- <%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/taglibs.jspf"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> - <body>
- <!--頭部 -->
- <divalign="center"
- <h1>頭部資訊:
- <sitemesh:writeproperty='title'/>
- </h1>
- </div>
- <hr>
- <!--左側選單欄 -->
- <divclass="conbgbtm">
- <divclass="leftbox">
- <ul>
- <li><ahref="#">選單1</a></li>
- <li><ahref="#">選單2</a></li>
- <li><ahref="#">選單3</a></li>
- </ul>
- </div>
- <sitemesh:writeproperty='body'></sitemesh:write>
- </div>
- <hr>
- <divalign="center">
- <span>Copyright © 2012-2013 廊坊資訊科技提高班 版權所有</span>
- </div>
- </body>
- </html>