sitemesh(简介)

[size=large][color=green]struts2之整合sitemesh(简介)[/color][/size]
sitemesh是一个非常优秀的页面装饰框架,sitemesh通过filter来截取request和response,然后给原始的页面加入一定的装饰(可能为header,footer等),然后把结果返回给客户端。对于被装饰的页面而言,完全无需关心本页面被装饰器装饰(装饰器被配置在配置文件中,由装饰器配置文件来控制哪个装饰器装饰哪些页面)。通过这种方式可以最大限度地实现页面代码的复用,并提供优秀的解耦。

sitemesh框架是opensymphony团队开发的javaEE框架之一,该框架的目的是页面的可维护性和复用性。sitemesh应用装饰器模式,用filter截取request和response,把装饰器页面中指定的公用内容插入到被装饰页面中,从而形成一个个完整页面。

通过使用sitemesh的页面装饰效果,我们就可以删除所有jsp页面的include指令,但达到与使用include指令相同的效果,并提供比include更好的解耦,更好的代码复用。

struts2可以通过sitemesh插件来与sitemesh框架整合,一旦strut2应用整合了sitemesh框架后,就可使用 sitemesh装饰页面来装饰应用中的其他页面。struts2对sitemesh框架进行了进一步封装,使用sitemesh装饰会更加简单。
[color=red]1.下载sitemesh-xxx.jar文件并复制到web应用的WEB0-INF/lib路径下。因为与struts2整合,所以我们还需要struts2-sitemesh-plugin-xxx.jar文件,[/color]([color=darkblue]这里我用的是sitemesh-2.4.2.jar[/color])

2.为了使sitemesh框架能够处理所有的用户请求,还必须在web.xml文件中配置sitemesh框架的核心Filter。配置sitemesh的核心Filter的配置片段如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>


3.为了在jsp页面中使用sitemesh的标签库,则还需要导入sitemesh标签库。如果使用Servlet2.4以上规范,则无需任何修改,直接在jsp页面中使用taglib指令导入标签库即可。
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>  
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>


[size=large][color=green]
4.定义装饰器页面[/color][/size]
装饰器页面就是一个普通的jsp页面,但这个页面包含了一些sitemesh标签。看下面的sitemesh装饰器页面代码:
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<html>
<head>
<title><decorator:title default="装饰器页面..." /></title>
<decorator:head />
</head>
<body>
<font size=8 color=red>这是一个Sitemesh的Demo</font>sitemesh的例子<hr>
<decorator:body />
<hr>
[align=center]sitemesh快速入门 [/align]
</body>
</html>

从上面页面代码中可以看出,该页面与传统jsp页面并没有太大的特别之处,只是该页面中包含了3个sitemesh标签:

. <decorator:title.../>:将被装饰页面的title部分插入该页面中。

. <decorator:head.../>:将被装饰页面的head部分插入该页面中。

. <decorator:body.../>:将被装饰页面的body部分插入该页面中。

sitemesh装饰页是整个应用的"母板页",被该装饰器装饰的页面都会使用该"母板页",从而保证所有页面看上去有相同的风格。"母板页"中包含了一些sitemesh标签,这些标签用于将被装饰页面的相应部分导入该"母板页"。

上面仅仅是定义了装饰器页面代码,还没有告诉sitemesh这个页面就是一个装饰器页面。为此,我们将该页面保存在web应用的decorators路径下。注:装饰器页面可以保存在web应用的任何路径,只要在配置文件中配置该路径即可。

5.下面在配置文件中配置该装饰器页面,配置装饰器页面使用decorators.xml文件,因此还需要增加一个decorators.xml文件,该文件的代码如下(该文件应放在/WEB-INF/下面):
<?xml version="1.0" encoding="GBK"?>
<!-- defaultdir指定装饰器文件所在的路径 -->
<decorators defaultdir="/decorators">
<!-- 在excludes元素下指定的页面将不会由sitemesh来装饰 -->
<excludes>
<pattern>/exclude.jsp</pattern>
<pattern>/exclude/*</pattern>
</excludes>
<!-- 指定main装饰器,该装饰器使用main.jsp页面 -->
<decorator name="main" page="main.jsp">
<!-- 使用main装饰器装饰所有的JSP页面 -->
<pattern>/*</pattern>
<pattern>/*.jsp</pattern>
</decorator>
<!-- 定义一个装饰器,但该装饰器默认不装饰任何页面 -->
<decorator name="panel" page="panel.jsp" />
</decorators>


6.在使用sitemesh框架还需要添加sitemesh配置文件sitemesh.xml 该文件的代码如下(该文件应放在/WEB-INF/下面):
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser default="true" class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>
<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>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}"/>
</mapper>
</decorator-mappers>
</sitemesh>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值