读书人

struts2.0与SiteMesh调整

发布时间: 2012-08-25 10:06:20 作者: rapoo

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前配置如下:

    ?struts2.0与SiteMesh调整struts2.0与SiteMesh调整
      <filter><filter-name>struts-prepare</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class></filter><filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter><filter> <filter-name>struts-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts-prepare</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping><filter-mapping><filter-name>struts-execute</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet> <servlet-name>sitemesh-freemarker</servlet-name> <servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class> <init-param> <param-name>default_encoding</param-name> <param-value>UTF-8</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>sitemesh-freemarker</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping><listener> <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class> </listener>

      ??? 注意:

      ??? 1. com.opensymphony.sitemesh.webapp.SiteMeshFilter这个过滤器是必需的,否则装饰不起作用

      ??? 2. 为了支持FreeMaker或Velocity,必须添加org.apache.struts2.sitemesh.FreemarkerDecoratorServlet这个Servlet,官方原话:

      ?<head>
      ??<title><decorator:title /></title>
      ??<decorator:head />
      ?</head>
      ?<body>
      ??<p>
      ???Add head decorator...
      ??</p>
      ??<decorator:body />
      ??<p>
      ???Add foot decorator...
      ??</p>
      ?</body>
      </html>

      ?

      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>

读书人网 >软件架构设计

热点推荐