簡單來講SiteMesh就是用來做網頁母版 Layout分層的。
注意:使用sitemesh與Struts整合後, sitemesh會默認攔截所有的action請求,需要在decorators.xml的配置文件中進行過濾配置,支持*號匹配符
這樣就需要合理進行action的配置!
例如: 不攔截所有/forwardAction_forward*的請求
<excludes>
<pattern>/index.jsp</pattern>
<pattern>/forwardAction_forward*</pattern>
</excludes>
安裝使用過程記錄:
1.把包扔到WEB-INF/lib下面。
包括兩個包: 一個是 sitemesh-2.4.1.jar
還有一個struts2的插件包struts2-sitemesh-plugin-2.2.1.1.jar
2.修改web.xml文檔:
之前:注意filter-class是StrutsPrepareAndExecuteFilter
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
之後:
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<!--sitemesh in the middle and after struts-prepare -->
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
註釋下面的代碼順序很重要sitemesh的過濾器要在prepare和execute之間。
3. 在WEB-INF下添加decorators.xml文檔:
<?xml version="1.0" encoding="utf-8"?>
<!-- defaultdir指定裝飾器文件所在的路徑 -->
<decorators defaultdir="/layout">
<!--excludes結點則指定了哪些路徑的請求不使用任何模板 -->
<excludes>
<pattern>/entpLogin.jsp</pattern>
<pattern>/entpRegister.jsp*</pattern>
<pattern>/messageDiv*</pattern>
<pattern>/registerServiceDetail*</pattern>
</excludes>
<!--decorator結點指定了模板的位置和文件名,通過pattern來指定哪些路徑引用哪個模板 -->
<decorator name="main" page="mode.jsp">
<pattern>/*</pattern>
</decorator>
</decorators
decorator標籤的page指定的是裝飾母版頁。至於<pattern>/messageDiv*</pattern>和<pattern>/registerServiceDetail*</pattern> 是我在頁面中使用Struts Ajax主題是指定的action(用來局部刷新的),這樣sitemesh就不對這些ajax返回的response進行裝飾
4.編寫母版mode.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"
prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<!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">
<link rel="stylesheet" type="text/css" href="css/style.css"
media="screen">
<title>
<decorator:title default="Welcome" /> <!--被裝飾頁面的Title-->
</title>
<sx:head />
<decorator:head /><!--被裝飾頁面的head-->
</head>
<body>
<decorator:body /><!--被裝飾頁面的body-->
</body>
</html>
Ok啦 這樣除了在decorator.xml中exclude的請求之外,所有的請求頁面都會被裝飾了,可以在<decorator:body /> 之前增加header,導航條,之後增加footer之類的東西啦!
注意:如果在頁面中增加了ajax主題的標籤(局部刷新),則一定要在exclude pattern中添加請求的action名,否則局部刷新得到的innerHtml也會被添加上header導航條footer等裝飾元素