SiteMesh介绍

一、介绍

SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。 Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,如htm的内容,使得它的内容也符合你的页面结构的要求。甚至于它能将HTML文件象include那样将该文件作为一个面板的形式嵌入到别的文件中去。所有的这些,都是GOF Decorator模式的最生动的实现。尽管它是由java语言来实现的,但它能与其他Web应用很好地集成。

下图是SiteMesh的工作原理图

112511_5BGb_817621.png

二、SiteMesh入门

集成SiteMesh到你的web项目

1.导入SiteMesh的jar包

 下载地址 http://wiki.sitemesh.org/wiki/display/sitemesh/Download

 或者使用Maven

                <dependency>
			<groupId>opensymphony</groupId>
			<artifactId>sitemesh</artifactId>
			<version>2.4.2</version>
		</dependency>

2.在web.xml中增加SiteMesh的filter

<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.创建decorators.xml配置文件文件

<?xml version="1.0" encoding="UTF-8"?>
<decorators>
</decorators>

部署web项目,启动不报错,说明已经成功将SiteMesh集成到项目中了。

使用SiteMesh

第一步:创建模版页面 basic-theme.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<!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 /></title>
</head>
<body>
    <h1>Header</h1>
    <decorator:body />
    <h1>Footer</b></h1>
</body>
</html>

第二行引入了SiteMesh的标签,标签的详细介绍将在后面写出。


第二步:定义装饰规则

修改decorators.xml来的定义要装饰哪些页面,使用哪个模版。

<?xml version="1.0" encoding="UTF-8"?>
<decorators>
	<decorator name="basic-theme" page="/decorators/basic-theme.jsp">
		<pattern>/data/*</pattern>
	</decorator>
</decorators>

好了,现在所有的/data下的页面都将被装饰成basic-theme.jsp的样子。

133755_Yg8u_817621.png


三、SiteMesh高级特性

1、理解SiteMesh的执行流程

在上面的简单例子中,我们省略了一个sitemesh.xml的配置文件,原因是sitemesh的jar包中提供了默认的sitemesh-default.xml。现在我们来重新梳理一遍它的配置。

web.xml中配置sitemesh的过滤器,这是sitemesh的入口,我们来看一个更为完整的配置

<filter>
		<filter-name>sitemesh</filter-name>
		<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
		<init-param>
			<param-name>configFile</param-name>
			<param-value>/WEB-INF/etc/sitemesh.xml</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>sitemesh</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

在filter中我们配置了自己的sitemesh.xml的配置文件,来看一下这个配置文件:

<sitemesh>
	<config-refresh seconds="36000" />
    <property name="decorators-file" value="/WEB-INF/decorators.xml"/>
    <excludes file="${decorators-file}"/>

	<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 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.FileDecoratorMapper"/>
		<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
			<param name="config" value="${decorators-file}" />
		</mapper>
	</decorator-mappers>
</sitemesh>

<excludes>标签告诉sitemesh我们定义排除文件的配置文件路径。

<page-parsers>定义了我们的解析器,你可以根据content-type的类型定义多个解析器。

<decorator-mappers>告诉sitemesh如何去匹配我们的页面与模版,也就是说我们可以通过多种方式告诉sitemesh哪个页面用哪个模版装饰。而最后一个ConfigDecoratorMapper就是我们上面简单例子中进行匹配的方式,参数param告诉了sitemesh我们的配置文件的位置(decorators.xml)。

我们再来看看decorators.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<decorators>
	<decorator name="main" page="main.jsp">
		<pattern>/*</pattern>
	</decorator>

	<excludes>
		<pattern>/plainPage.jsp</pattern>
		<pattern>/plain/*.jsp</pattern>
	</excludes>
</decorators>

该配置文件只有在ConfigDecoratorMapper之前所有的mapper都没能够找到模版页面的时候才会起作用。


上面sitemesh.xml中只定义了部分mapper,所有的mapper见下图,想了解每个mapper的作用建议查看官方文档。

151928_MgJT_817621.png

执行流程总结为下图:

152129_F5mq_817621.png

2、Decorator Tags汇总

<decorator:head />

<decorator:body />

<decorator:title [ default="..." ] />

<decorator:getProperty property="..." [ default="..." ] [ writeEntireProperty="..." ]/>
For example:
The decorator: <body bgcolor="White"<decorator:getProperty property="body.onload" writeEntireProperty="true" />>
The undecorated page: <body onload="document.someform.somefield.focus();">
The decorated page: <body bgcolor="White" onload="document.someform.somefield.focus();">
该标签可用于模版页面从被装饰页面取属性

<decorator:usePage id="..." />

<page:applyDecorator name="..." [ page="..." title="..." ] >
   .....
</page:applyDecorator>

<page:param name="..."> ... </page:param>


转载于:https://my.oschina.net/yuanjifeng/blog/630288

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值