您的位置:首页 > 其它

SiteMesh介绍

2015-04-01 17:56 141 查看
转自:http://javauu.com/thread-27-1-1.html


一、SIteMesh介绍

一、SiteMesh简介

下载

二、SiteMesh原理

SiteMesh框架是OpenSymphony团队开发的一个非常优秀的页面装饰器框架,它通过对用户请求进行过滤,并对服务器向客户端响应也进行过滤,然后给原始页面加入一定的装饰(header,footer等),然后把结果返回给客户端。通过SiteMesh的页面装饰,可以提供更好的代码复用,所有的页面装饰效果耦合在目标页面中,无需再使用include指令来包含装饰效果,目标页与装饰页完全分离,如果所有页面使用相同的装饰器,可以是整个Web应用具有统一的风格。

三、SiteMesh简单例子

接下来通过一个SiteMesh简单例子来了解SiteMesh的功能:

将sitemesh-2.3.jar放 到 [web-app]/WEB-INF/lib目录下;

在[web-app]/WEB-INF/新建一个decorators.xml文件,包含以下内容:


普通浏览复制代码打印代码关于程序

<?xml version="1.0" encoding="utf-8"?>

<decorators defaultdir="/decorators">

<!-- 此处用来定义不需要过滤的页面 -->

<excludes>

</excludes>

<!-- 用来定义装饰器要过滤的页面 -->

<decorator name="main" page="main.jsp">

<pattern>/*</pattern>

</decorator>

</decorators>

Xml代码


<?xml version="1.0" encoding="utf-8"?>

<decorators defaultdir="/decorators">

<!-- 此处用来定义不需要过滤的页面 -->

<excludes>

</excludes>

<!-- 用来定义装饰器要过滤的页面 -->

<decorator name="main" page="main.jsp">

<pattern>/*</pattern>

</decorator>

</decorators>

在[web-app]/WEB-INF/web.xml添加以下内容:


普通浏览复制代码打印代码关于程序

<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>

Xml代码


<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>

在[web-app]下创建一个decorators文件夹,在该文件下再创建一个装饰页面main.jsp,包含以下内容:


普通浏览复制代码打印代码关于程序

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<!-- 第一个装饰页面 -->

<head>

<!-- 从被装饰页面获取title标签内容,并设置默认值-->

<title><decorator:title default="默认title"/></title>

<!-- 从被装饰页面获取head标签内容 -->

<decorator:head/>

</head>

<body>

<h2>SiteMesh装饰header</h2>

<hr />

<!-- 从被装饰页面获取body标签内容 -->

<decorator:body />

<hr />

<h2>SiteMesh装饰footer</h2>

</body>

</html>

Html代码


<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<!-- 第一个装饰页面 -->

<head>

<!-- 从被装饰页面获取title标签内容,并设置默认值-->

<title><decorator:title default="默认title"/></title>

<!-- 从被装饰页面获取head标签内容 -->

<decorator:head/>

</head>

<body>

<h2>SiteMesh装饰header</h2>

<hr />

<!-- 从被装饰页面获取body标签内容 -->

<decorator:body />

<hr />

<h2>SiteMesh装饰footer</h2>

</body>

</html>

在[web-app]下创建被装饰页面index.jsp,包含以下内容:


普通浏览复制代码打印代码关于程序

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<!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">

<title>被装饰(目标)页面title</title>

</head>

<body>

<h4>被装饰(目标)页面body标签内内容。</h4>

<h3>使用SiteMesh的好处?</h3>

<ul>

<li>被装饰(目标)页面和装饰页面完全分离。</li>

<li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>

<li>更容易实现统一的网站风格。</li>

<li>还有。。。</li>

</ul>

</body>

</html>

Html代码


<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<!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">

<title>被装饰(目标)页面title</title>

</head>

<body>

<h4>被装饰(目标)页面body标签内内容。</h4>

<h3>使用SiteMesh的好处?</h3>

<ul>

<li>被装饰(目标)页面和装饰页面完全分离。</li>

<li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>

<li>更容易实现统一的网站风格。</li>

<li>还有。。。</li>

</ul>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: