转自:http://javauu.com/thread-36-1-1.html
、SiteMesh在JSP中页面装饰
通常情况下,在一个工程中,我们不得不使用include标签在每个jsp页面中来包含各种header, stylesheet, scripts, footer。现在,在sitemesh的帮助下,不需要在每个页面中使用include标签,并可实现相同效果。
接下来,完成一个SiteMesh与JSP结合的实例,并且添加打印装饰。
- <?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>
- <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>
- <?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>
- <%@ 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>
- <%@ 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>
- <%@ 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>
- <%@ 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>
- <%@ 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>