【SSI开发总结.5】SiteMesh装饰器

SiteMesh项目简介

OS(OpenSymphony)的SiteMesh是一个用来在JSP中实现页面布局和装饰(layout and decoration)
的框架组件,能够帮助网站开发人员较容易实现页面中动态内容和静态装饰外观的分离。
Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大
量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。
它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,如htm的内容,
使得它的内容也符合你的页面结构的要求。甚至于它能将HTML文件象include那样将该文件作为一个面板
的形式嵌入到别的文件中去。所有的这些,都是GOF的Decorator模式的最生动的实现。尽管它是由java语言来实现的,但它能与其他Web应用很好地集成。
一个请求到服务器后,如果该请求需要sitemesh装饰,服务器先解释被请求的资源,然后根据配置文件获得用于该请求的装饰器,最后用装饰器装饰被请求资源,将结果一同返回给客户端浏览器。

1.配置WEB.XML

/******* /WEB-INF/web.xml******/

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<display-name>game_proj</display-name>
<!-- 中文语言支持过滤器 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.popoann.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<!-- Acegi过滤器 -->
<filter-name>acegiFilterChain</filter-name>
<filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
<init-param>
<param-name>targetClass</param-name>
<param-value>org.acegisecurity.util.FilterChainProxy</param-value>
</init-param>
</filter>
<!-- Acegi过滤器URL映射 -->
<filter-mapping>
<filter-name>acegiFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ActionContextCleanUp过滤器 -->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- SiteMesh的核心过滤器 -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Struts核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>htm</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 应用程序启动时,加载SpringIoC容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/beans-*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 欢迎页面 -->
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>

</web-app>

 

注意过滤器的顺序,ActionContextCleanUp过滤器 -> SiteMesh的核心过滤器 -> Struts核心过滤器

2.配置SITEMESH.XML

/******* /WEB-INF/sitemesh.xml******/

<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.DefaultPageParser" />
<parser content-type="text/html"
class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
<parser content-type="text/html;charset=gb2312"
class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
</page-parsers>
<!--
<page-parsers>
<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
</page-parsers>
-->
<decorator-mappers>

<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" />
<param name="property.2" value="decorator" />
</mapper>

<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
</mapper>

<mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
<param name="match.MSIE" value="ie" />
<param name="match.Mozilla [" value="ns" />
<param name="match.Opera" value="opera" />
<param name="match.Lynx" value="lynx" />
</mapper>

<mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
<param name="decorator" value="printable" />
<param name="parameter.name" value="printable" />
<param name="parameter.value" value="true" />
</mapper>

<mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
<param name="decorator" value="robot" />
</mapper>

<mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
<param name="decorator.parameter" value="decorator" />
<param name="parameter.name" value="confirm" />
<param name="parameter.value" value="true" />
</mapper>

<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
</mapper>

<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>

</decorator-mappers>

</sitemesh>

 

本配置文件直接拷贝自下载的sitemesh开发包,基本上只需要修改

<property name="decorators-file" value="/WEB-INF/decorators.xml"/>

这个标签,用来指定对页面进行装配的配置文件路径

3.配置DECORATORS.XML

/******* /WEB-INF/decorators.xml******/

<?xml version="1.0" encoding="ISO-8859-1"?>

<decorators defaultdir="/decorators">
<!-- Any urls that are excluded will never be decorated by Sitemesh -->

<excludes>
<pattern>/about.htm</pattern>
</excludes>

<decorator name="main1" page="headAndFoot1.jsp">
<pattern>/index.htm</pattern>
</decorator>

<decorator name="main2" page="headAndFoot2.jsp">
<pattern>/myspace/*</pattern>
</decorator>
</decorators>

 

decorators标签的 defaultdir属性指定修饰页面相对于应用程序根目录的位置,这里为 "/decorators"

excludes标签:指定哪些页面不被修饰

decorator指定哪些匹配页面被修饰,支持通配符,page属性表示修饰页面,即给页面穿什么样的衣服

4.设计修饰页面

/****** /decorators/headAndFoot1.jsp ******/

<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title><decorator:title default="kangxm test" /> </title>
<decorator:head/>
</head>

<body>
<decorator:body/>
</body>
</html>

 

红色标示表示需要配置的标签:

<%@taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>

确保本页面被纳入SiteMesh体系

<decorator:title default="kangxm test" />

提取被装饰页面的标题,如果没有指定,则使用“kangxm test”

<decorator:head/>

提取被装饰页面head标签中的内容

<decorator:body/>

提取被装饰页面body标签中的内容

/*************************************************************

到此,SiteMesh配置结束,总得来说,配置比较容易,使页面控制更灵活,同时,也避免了重复性代码的出现。

【注意】:要使sitemesh能与struts2集成,还必须下载struts2的sitemesh插件

你可能感兴趣的:(sitemesh)