JSP中使用SIteMesh实现类似asp.net母版效果
2015-07-18 11:58
676 查看
1.介绍
SiteMesh的是应用Decorator模式,以允许从表现内容的完全分离一个轻量级的,灵活的Java Web应用程序框架。
siteMesh框架是OpenSymphony团队开发的一个非常优秀的页面装饰器框架,它通过对用户请求进行过滤,并对服务器向客户端响应也进行过滤,然后给原始页面加入一定的装饰(header,footer等),然后把结果返回给客户端。通过SiteMesh的页面装饰,可以提供更好的代码复用,所有的页面装饰效果耦合在目标页面中,无需再使用include指令来包含装饰效果,目标页与装饰页完全分离,如果所有页面使用相同的装饰器,可以是整个Web应用具有统一的风格。
步骤1
引入JAR包sitemesh-3.0.1.jar
步骤2
WEB-INF目录下创建sitemesh3.xml
步骤3
WEB-INF目录下web.xml中加入sitemesh
步骤4
建立母版页面master.jsp
步骤5
建立测试页面 test.jsp
步骤6 完成,效果如下
sitemesh3.xml 配置详解
SiteMesh的是应用Decorator模式,以允许从表现内容的完全分离一个轻量级的,灵活的Java Web应用程序框架。
siteMesh框架是OpenSymphony团队开发的一个非常优秀的页面装饰器框架,它通过对用户请求进行过滤,并对服务器向客户端响应也进行过滤,然后给原始页面加入一定的装饰(header,footer等),然后把结果返回给客户端。通过SiteMesh的页面装饰,可以提供更好的代码复用,所有的页面装饰效果耦合在目标页面中,无需再使用include指令来包含装饰效果,目标页与装饰页完全分离,如果所有页面使用相同的装饰器,可以是整个Web应用具有统一的风格。
步骤1
引入JAR包sitemesh-3.0.1.jar
步骤2
WEB-INF目录下创建sitemesh3.xml
<?xml version="1.0" encoding="UTF-8"?> <sitemesh> <!-- 指明满足“/*”的页面,将被“/master.jsp”所装饰 --> <mapping path="/*" decorator="/master.jsp" /> <!-- 指明满足“/exclude.jsp*”的页面,将被排除,不被装饰 --> <mapping path="/el.jsp*" exclue="true" /> </sitemesh>
步骤3
WEB-INF目录下web.xml中加入sitemesh
<filter> <filter-name>sitemesh</filter-name> <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
步骤4
建立母版页面master.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><sitemesh:write property='title'/></title> <sitemesh:write property='head'/> </head> <body> <h1 class='title'>SiteMesh example site: <sitemesh:write property='title'/></h1> <hr> <sitemesh:write property='body'/> </body> </html>
步骤5
建立测试页面 test.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> </head> <body> <p>sitemesh应用Decorator模式,用filter截取request和response,把页面组件head,content,banner结合为一个完整的视图。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet,scripts and footer,现在,在sitemesh的帮助下,我们可以开心的删掉他们了。如下图,你想轻松的达到复合视图模式,那末看完本文吧。</p> </body> </html>
步骤6 完成,效果如下
sitemesh3.xml 配置详解
<sitemesh> <!--默认情况下, sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰, 我们可以添加更多的 mime 类型--> <mime-type>text/html</mime-type> <mime-type>application/vnd.wap.xhtml+xml</mime-type> <mime-type>application/xhtml+xml</mime-type> ... <!-- 默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰 --> <mapping decorator="/default-decorator.html"/> <!-- 对不同的路径,启用不同的装饰器 --> <mapping path="/admin/*" decorator="/another-decorator.html"/> <mapping path="/*.special.jsp" decorator="/special-decorator.html"/> <!-- 对同一路径,启用多个装饰器 --> <mapping> <path>/articles/*</path> <decorator>/decorators/article.html</decorator> <decorator>/decorators/two-page-layout.html</decorator> <decorator>/decorators/common.html</decorator> </mapping> <!-- 排除,不进行装饰的路径 --> <mapping path="/javadoc/*" exclue="true"/> <mapping path="/brochures/*" exclue="true"/> <!-- 自定义 tag 规则 --> <content-processor> <tag-rule-bundle class="com.something.CssCompressingBundle" /> <tag-rule-bundle class="com.something.LinkRewritingBundle"/> </content-processor> ... </sitemesh>
相关文章推荐
- 检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(转)
- Js 给asp.net 控件赋值
- asp.net页面之间的跳转
- ASP.NET中将声音文件添加到资源中并进行播放的方法
- ASP.NET 3.5 MVC框架深度解析
- 【asp.net】常用内置对象(Request,Response、server、Cookies)
- ASP.NET - Web.config文件详解
- ASP.NET - 出错页
- ASP.NET - 在线编辑器(FreeTextBox)
- 【asp】【07】asp判断session是否存在的四种方法
- [ASP.NET MVC 小牛之路]02 - C#知识点提要
- JasperReport学习笔记(一)
- [ASP.NET MVC 大牛之路]01 - C#高级知识点概要(1) - 委托和事件
- 这算是ASP.NET MVC的一个大BUG吗?
- asp.net mvc多级目录结构和多级area实现技巧
- 【工作日志】【asp】【06】rs.bof与 rs.eof区别
- [ASP.NET MVC 大牛之路]02 - C#高级知识点概要(2) - 线程和并发
- ASP.NET应用程序与页面生命周期
- 问题.NET--win7 IIS唯一密钥属性“VALUE”设置为“DEFAULT.ASPX”时,无法添加类型为“add”的重复集合
- ASP.NET 中Post与Get提交方法的不同