sitemesh是表現層的技術,它將公用的部分抽出來了,就不用再頁面中加入大量的<include>標籤了
1.
在/WEB-INF/lib中加入sitemesh的jar包,這裏使用sitemesh-2.4.1.jar
2.在web.xml中加入sitemesh的過慮器:
<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>
3.在/WEB-INF下添加一個名爲decorators.xml的文件,內容爲:
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<decorator name="main" page="default.jsp">
<pattern>*</pattern>
</decorator>
</decorators>
4.在webroot下加入decorator文件夾,並加入一個文件default.jsp,內容爲:
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<html>
<head>
<title>
<!--把請求的原始頁面的body內的全部內容插入到相應位置-->
<!-- 如果頁面中沒有title就用default設置的 -->
<decorator:title default="test sitemesh" />
</title>
<decorator:head />
</head>
<body>
<center>
<font color="red" size="7">top</font><!-- 頭部 -->
<hr>
<decorator:body /> <!-- 把請求的原始頁面的body內的全部內容插入到相應位置 -->
<hr>
<font color="red" size="7">bottom</font><!-- 尾部 -->
</center>
</body>
</html>
這樣就已經完成了一個最簡單的實例了,訪問項目中的任何一個頁面就可以看到效果了 ^_^