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. filter>  
  2.    <filter-name>ActionContextCleanUp</filter-name>  
  3.    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>  
  4. </filter>  
  5. <filter>  
  6.    <filter-name>sitemesh</filter-name>  
  7.    <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>  
  8. </filter>  
  9. <filter>  
  10.    <filter-name>struts2</filter-name>  
  11.    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  12. </filter>  

 从struts2.1.3开始ActionContextCleanUp 和 FilterDispatcher过滤器,已经不建议使用了。将使用StrutsPrepareFilter 和StrutsExecuteFilter拦截器替代。正确的配置方式如下:

 

Xml代码 复制代码  收藏代码
  1. <filter>  
  2.         <filter-name>struts-prepare</filter-name>  
  3.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>  
  4.     </filter>  
  5.     <filter>  
  6.         <filter-name>sitemesh</filter-name>  
  7.         <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>  
  8.     </filter>  
  9.     <filter>  
  10.         <filter-name>struts-execute</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>  
  12.     </filter>  
  13.        
  14.     <filter-mapping>  
  15.         <filter-name>struts-prepare</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18.     <filter-mapping>  
  19.         <filter-name>sitemesh</filter-name>  
  20.         <url-pattern>/*</url-pattern>  
  21.         <dispatcher>REQUEST</dispatcher>  
  22.         <dispatcher>FORWARD</dispatcher>  
  23.         <dispatcher>INCLUDE</dispatcher>  
  24.     </filter-mapping>  
  25.     <filter-mapping>  
  26.         <filter-name>struts-execute</filter-name>  
  27.         <url-pattern>/*</url-pattern>  
  28.     </filter-mapping>  
  29.        
  30.     <servlet>  
  31.         <servlet-name>sitemesh-freemarker</servlet-name>  
  32.         <servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class>  
  33.         <init-param>  
  34.             <param-name>default_encoding</param-name>  
  35.             <param-value>UTF-8</param-value>  
  36.         </init-param>  
  37.         <load-on-startup>1</load-on-startup>  
  38.     </servlet>  
  39.     <servlet-mapping>  
  40.         <servlet-name>sitemesh-freemarker</servlet-name>  
  41.         <url-pattern>*.ftl</url-pattern>  
  42.     </servlet-mapping>  
  43.     <listener>  
  44.         <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>  
  45.     </listener>  
<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,官方原话:

写道
From 2.2+ the recommended way to use Freemarker with Sitemesh is through the org.apache.struts2.sitemesh.FreemarkerDecoratorServlet servlet

    3. 而已从2.2.x开始,必须添加org.apache.struts2.dispatcher.ng.listener.StrutsListener这个监听器

写道
From 2.2+ the new com.opensymphony.sitemesh.webapp.SiteMeshFilter filter and Struts org.apache.struts2.dispatcher.ng.listener.StrutsListener context listener must be added to web.xml,

 

4.在webroot目錄下面建decorators文件夾,裡面放裝飾的模型main.jsp(decorators是装饰文件默认路径)

<%@ page contentType="text/html;charset=UTF-8"%>
<%@taglib prefix="decorator"
 uri="http://www.opensymphony.com/sitemesh/decorator"%>> //导入sitemesh标签库
<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%<html>
 <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>

你可能感兴趣的:(apache,freemarker,xml,servlet,struts)