struts2.0与SiteMesh整合
1.在工程中引入SiteMesh的必要jar包,和struts2-sitemesh-plugin-2.0.8.jar(该包在strut2.0的lib目录中可以找到);
?
2.Struts2的所有值是保存在Stack Context或者ValueStack中的,默认情况是,
某个过滤器一旦访问了该Stack Context或ValueStack后,里面对应的值将会被清除掉,
如果先使用Struts2的FilterDispatcher来过滤用户请求,
则SiteMesh的过滤器将无法取得Stack Context或者ValueStack中的数据。
为了解决整个问题,Struts2提供了ActionContextCleanUp类。在Struts2的架构中,
标准的过滤器链(filter-chain)一般以 ActionContextCleanUp 开始,后面跟着其他需要的过滤器,
最后,由 FilterDispatcher来处理请求,FilterDispatcher通常是将请求传递给ActionMapper
但问题是:ActionContextCleanUp过滤器只能保证在FilterDispatcher之前先不清除Stack Context和ValueStack中的值。
如果将SiteMesh过滤器排在FilterDispatcher之后,这会导致SiteMesh过滤器无法访问到Stack Context和ValueStack中的值。
因此,为了让SiteMesh过滤器和FilterDispatcher都可访问到Stack Context和ValueStack中的值,
且FilterDispatcher可以在合适时机清除Stack Context和ValueStack中的值,应该使用如下的过滤器顺序:
(1)ActionContextCleanUp过滤器。
(2)SiteMesh核心过滤器。
(3)FilterDispatcher过滤器。
?
3.
? struts2.13前配置如下:
??? 注意:
??? 1. com.opensymphony.sitemesh.webapp.SiteMeshFilter这个过滤器是必需的,否则装饰不起作用
??? 2. 为了支持FreeMaker或Velocity,必须添加org.apache.struts2.sitemesh.FreemarkerDecoratorServlet这个Servlet,官方原话:
?<head>?
5.配置decorators-file文件.該文件名称和路径为sitemesh-default.xml中 <property name="decorators-file" value="/WEB-INF/decorators.xml"/>配置的名称.所以该文件必须放在WEB-INF目录下.名称为decorators.xml.
decorators.xml配置如下:
<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators"> //其中/decorators是装饰文件默认路径.也可以配置为其他路径
<!--excludes結點則指定了哪些路徑的請求不使用任何模板-->
<excludes>
<pattern>/index.jsp*</pattern>
<pattern>/login/*</pattern>
</excludes>
<!--decorator結點指定了模板的位置和文件名,通過pattern來指定哪些路徑引用哪個模板-->
<decorator name="main" page="mode.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>