SiteMesh模板应用与struts2整合

SiteMesh是一个非常优秀的页面装饰框架,通过SiteMeth的帮助,可以大大提高页面布局的开发速度,并且统一应用的所有页面的整体布局。它与Jsp中的Include指令完全相同的效果,但提供了比include更好的解耦、更好的代码复用。

jar包:

sitemesh.jar struts2-sitemesh-plugin.jar

1、web.xml文件过滤配置

<?xml version="1.0" encoding="GBK"?>
<!-- 配置Web应用配置文件的根元素,并指定配置文件的Schema信息 -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
	<!-- 定义ActionContextCleanUp过滤器 -->
	<filter>
		<filter-name>struts-cleanup</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
	</filter>
	<!-- 定义SiteMesh的核心过滤器 -->
	<filter>
		<filter-name>sitemesh</filter-name>
		<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
	</filter>
	<!-- 定义Struts 2的核心过滤器 -->
	<filter>
		<filter-name>struts</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>

	<!-- 定义过滤器链 -->
	<!-- 排在第一位的过滤器是:ActionContextCleanUp过滤器 -->
	<filter-mapping>
		<filter-name>struts-cleanup</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 排在第二位的过滤器是:SiteMesh的核心过滤器 -->
	<filter-mapping>
		<filter-name>sitemesh</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 排在第三位的过滤器是:FilterDispatcher过滤器 -->
	<filter-mapping>
		<filter-name>struts</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 定义一个Listener,该Listener在应用启动时创建Spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- 定义一个Listener,该Listener在应用启动时加载MyFaces的Context -->
	<listener>
		<listener-class>org.apache.myfaces.webapp.StartupServletContextListener
		</listener-class>
	</listener>
	<!-- 配置JSF的FacesServlet,让其在应用启动时加载 -->
	<servlet>
		<servlet-name>faces</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- 让FacesServlet拦截所有以*.action结尾的请求 -->
	<servlet-mapping>
		<servlet-name>faces</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
</web-app>

  2、模板页面 main.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"
	prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page"
	prefix="page"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><decorator:title default="SiteMesh的装饰器页"/></title>
	<link href="decorators/main.css" rel="stylesheet" type="text/css">
	<decorator:head/>
</head>
<body>
<table width="100%" height="100%">
	<tr>
		<td valign="top">
		<!-- 引入一个页面,临时指定所用的装饰器 -->
		<page:applyDecorator page="/book.html" name="panel" />
		<page:applyDecorator page="/link.html" name="panel" />
		</td>
		<td width="100%">
			<table width="100%" height="100%">
				<tr>
					<td id="pageTitle">
						<decorator:title/>
					</td>
				</tr>
				<tr>
					<td valign="top" height="100%">
						<decorator:body />
					</td>
				</tr>
				<tr>
					<td id="footer">
						<b>被包含的内容</b><br />
						SithMesh提供页面装饰支持
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</body>
</html>

  3、配置装饰器decorators.xml

<?xml version="1.0" encoding="GBK"?>
<decorators defaultdir="/decorators">
    <!-- 在excludes元素下指定的页面将不会由SiteMesh来装饰 -->
    <excludes>
        <pattern>/exclude.jsp</pattern>
        <pattern>/exclude/*</pattern>
    </excludes>
	<!-- 创建一个名为main的装饰器,该装饰器页面为main.jsp,
	     用于装饰pattern指定的URL的所有页面-->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
	<!-- 定义一个装饰器,但该装饰器默认不装饰任何页面 -->
    <decorator name="panel" page="panel.jsp"/>
</decorators>
 

猜你喜欢

转载自zfei.iteye.com/blog/1271543