转自:http://javauu.com/thread-36-1-1.html
、SiteMesh在JSP中页面装饰
一、SiteMesh在JSP页面中使用
二、SiteMesh在JSP页面中使用实例
将sitemesh-2.3.jar放 到 [web-app]/WEB-INF/lib目录下;
在[web-app]/WEB-INF/新建一个decorators.xml文件,包含以下内容:
通常情况下,在一个工程中,我们不得不使用include标签在每个jsp页面中来包含各种header, stylesheet, scripts, footer。现在,在sitemesh的帮助下,不需要在每个页面中使用include标签,并可实现相同效果。
二、SiteMesh在JSP页面中使用实例
接下来,完成一个SiteMesh与JSP结合的实例,并且添加打印装饰。
将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> <!-- 过滤掉error.jsp页面 --> <pattern>/error.jsp</pattern> </excludes> <!-- 用来定义装饰器要过滤的页面 --> <decorator name="main" page="main.jsp"> <pattern>/*</pattern> </decorator> <!-- 用来定义装饰器要过滤的打印页面 --> <decorator name="printable1" page="printable1.jsp"/> </decorators>
<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
<!-- 此处用来定义不需要过滤的页面 -->
<excludes>
<!-- 过滤掉error.jsp页面 -->
<pattern>/error.jsp</pattern>
</excludes>
<!-- 用来定义装饰器要过滤的页面 -->
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
<!-- 用来定义装饰器要过滤的打印页面 -->
<decorator name="printable1" page="printable1.jsp"/>
</decorators>
在/WEB-INF/新建一个sitemesh.xml文件,包含内容如下:
<sitemesh> <property name="decorators-file" value="/WEB-INF/decorators.xml" /> <excludes file="${decorators-file}" /> <!-- Page Parsers :负责读取stream的数据到一个Page对象中以被SiteMesh解析和操作。--> <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=utf-8" class="com.opensymphony.module.sitemesh.parser.FastPageParser" /> </page-parsers> <decorator-mappers> <!-- 可打印的装饰器,可以允许你当用http://localhost/test.html?printable=true方式访问时给出原始页面以供打印 --> <!-- 其中printable1 与 decorators.xml中的装饰器printable1一致进行映射,若不添加此段,默认为printable --> <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper"> <param name="decorator" value="printable1" /> <param name="parameter.name" value="printable" /> <param name="parameter.value" value="true" /> </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" />
<excludes file="${decorators-file}" />
<!-- Page Parsers :负责读取stream的数据到一个Page对象中以被SiteMesh解析和操作。-->
<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=utf-8"
class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
</page-parsers>
<decorator-mappers>
<!-- 可打印的装饰器,可以允许你当用http://localhost/test.html?printable=true方式访问时给出原始页面以供打印 -->
<!-- 其中printable1 与 decorators.xml中的装饰器printable1一致进行映射,若不添加此段,默认为printable -->
<mapper
class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
<param name="decorator" value="printable1" />
<param name="parameter.name" value="printable" />
<param name="parameter.value" value="true" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>
在[web-app]/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 [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]"> <display-name>SiteMeshDemoL2</display-name> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?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 [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]">
<display-name>SiteMeshDemoL2</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在[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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><decorator:title default="默认title"/>--javauu.com</title> <decorator:head/> </head> <body> <div id="header"><jsp:include page="header.jsp" flush="true"/></div> <div id="body"><decorator:body /></div> <p><small>(<a href="?printable=true">printable version</a>)</small></p> <div id="footer"><jsp:include page="footer.jsp" flush="true"/></div> </body> </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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><decorator:title default="默认title"/>--javauu.com</title>
<decorator:head/>
</head>
<body>
<div id="header"><jsp:include page="header.jsp" flush="true"/></div>
<div id="body"><decorator:body /></div>
<p><small>(<a href="?printable=true">printable version</a>)</small></p>
<div id="footer"><jsp:include page="footer.jsp" flush="true"/></div>
</body>
</html>
在decorators文件夹下再创建一个装饰页面printable1.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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><decorator:title default="默认title"/>--javauu.com</title> <decorator:head/> </head> <body> <h5>这是要打印的装饰页面,去掉header,footer</h5> <div id="body"><decorator:body /></div> <h5>打印...</h5> </body> </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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><decorator:title default="默认title"/>--javauu.com</title>
<decorator:head/>
</head>
<body>
<h5>这是要打印的装饰页面,去掉header,footer</h5>
<div id="body"><decorator:body /></div>
<h5>打印...</h5>
</body>
</html>
在decorators文件夹下再创建一个用于被装饰页面包含的header.jsp和footer.jsp,分别包含以下内容:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <h1>全局统一header</h1> <h2>好久没有人把牛皮吹的这么清新脱俗了!</h2> <hr />
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<h1>全局统一header</h1>
<h2>好久没有人把牛皮吹的这么清新脱俗了!</h2>
<hr />
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <hr /> <h1>全局统一footer</h1> <h2>爸爸今天打了我两次,第一次是因为看见了我手里两分的成绩单,第二次是因为成绩单是他小时候的。</h2>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<hr />
<h1>全局统一footer</h1>
<h2>爸爸今天打了我两次,第一次是因为看见了我手里两分的成绩单,第二次是因为成绩单是他小时候的。</h2>
在[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>index</title> </head> <body> <h1>被装饰过的首页</h1> <h2><a href="result.jsp">点击我</a>跳转到被装饰过结果页面!</h2> <h2><a href="error.jsp">点击我</a>跳转到被排除装饰的错误页面!</h2> <h2>有两个人掉到陷阱里了,死的人叫死人,活人叫什么? <a href="answer.jsp">答案</a></h2> </body> </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>index</title>
</head>
<body>
<h1>被装饰过的首页</h1>
<h2><a href="result.jsp">点击我</a>跳转到被装饰过结果页面!</h2>
<h2><a href="error.jsp">点击我</a>跳转到被排除装饰的错误页面!</h2>
<h2>有两个人掉到陷阱里了,死的人叫死人,活人叫什么? <a href="answer.jsp">答案</a></h2>
</body>
</html>
在[web-app]下创建被装饰页面error.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>error</title> </head> <body> <h1>错误页面!!!</h1> <h2>珍惜生活——上帝还让你活着,就肯定有他的安排。</h2> </body> </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>error</title>
</head>
<body>
<h1>错误页面!!!</h1>
<h2>珍惜生活——上帝还让你活着,就肯定有他的安排。</h2>
</body>
</html>
在[web-app]下创建被装饰页面answer.jsp,包含以下内容: