Srping2.5+struts2.1+Ibatis2.3_SiteMesh

来源:互联网 发布:黑人国家 知乎 编辑:程序博客网 时间:2024/06/10 04:07

需要引进的包:

struts2-core-2.0.11.jar(struts2框架)
commons-lang.jar (struts2基础设施1,提供java常用操作API,比如字符串处理,XML解析等)
commons-logging.jar(struts2基础设施2,提供java日志操作API,抽象出日志接口,方便操作)
freemarker-2.3.8.jar(struts2基础设施3,freemarker模板视图支持,struts2标签主题支持)
xwork-2.0.4.jar(struts2基础设施4命令模式框架支持
ognl-2.6.11.jar(struts2基础设施5,OGNL表达式支持)
struts2-spring-plugin-2.0.11.jar (struts2基础设施6,struts集成spring插件)
struts2-sitemesh-plugin-2.0.11.jar(struts2基础设施7,struts集成sitemesh插件)
spring.jar(spring2.5框架)
ibatis-2.3.0.677.jar (ibatis框架)
sitemesh-2.3.jar(sitemesh框架)
以下文件上传需要的jar:
commons-codec.jar
commons-fileupload-1.2.jar
commons-io-1.4.jar

 

  核心配置

 <!-- 定义ActionContextCleanUp过滤器 -->
  <filter>
   <filter-name>struts-cleanup</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  </filter>
 
   <!-- 定义SiteMesh的核心过滤器 -->
   <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>
             com.opensymphony.module.sitemesh.filter.PageFilter
        </filter-class>
    </filter>
   <!-- 定义Struts2的核心过滤器 -->
    <filter>
     <filter-name>struts</filter-name>
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
 
     <!-- 定义过滤器链 -->
     <!-- 排在第一位的过滤器是:ActionContextCleanUp过滤器。 -->
    <filter-mapping>
      <filter-name>struts-cleanup</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  
    <!-- 排在第二位的过滤器是:SiteMesh核心过滤器。 -->
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   
    <!-- 排在第三位的过滤器是:FilterDispatcher过滤器。 -->
    <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

 

上面就是一些基本配置

 

  为了整合SiteMesh框架必须在web.xml文件中配置SiteMesh的核心过滤器,让该核心过滤器来过滤所有的用户请求。但我们知道,Struts2的所有值是保存在Stack Context或者ValueStack中的,默认情况是,某个过滤器一旦访问了该StackContext或ValueStack后,里面对应的值将会被清除掉,如果先使用Struts2的FilterDispatcher来过滤用户请求,则SiteMesh的过滤器将无法取得Stack Context或者ValueStack中的数据。
为了解决整个问题,Struts2提供了ActionContextCleanUp类。在Struts2的架构中,标准的过滤器链(filter-chain)一般以 ActionContextCleanUp 开始,后面跟着其他需要的过滤器,最后,由FilterDispatcher来处理请求,FilterDispatcher通常是将请求传递给ActionMapper。
ActionContextCleanUp 的一个重要作用是整合SiteMesh页面装饰器,它通知FilterDispatcher在正确的时间清楚ActionContext中的请求数据。
注意:如果需要在SiteMesh的修饰器页面中访问ActionContext,ActionContextCleanUp 过滤器必须放在过滤器链的起点。
ActionContextCleanUp过滤器用来与FilterDispatcher协同工作来整合SiteMesh,通常,我们会把把ActionContextUp过滤器排在第一位,似乎将FilterDispatcher排在第二位是较好解决方案。
但问题是:ActionContextCleanUp过滤器只能保证在FilterDispatcher之前先不清除StackContext和ValueStack中的值。如果将SiteMesh过滤器排在FilterDispatcher之后,这会导致SiteMesh过滤器无法访问到Stack Context和ValueStack中的值。
因此,为了让SiteMesh过滤器和FilterDispatcher都可访问到StackContext和ValueStack中的值,且FilterDispatcher可以在合适时机清除StackContext和ValueStack中的值,应该使用如下的过滤器顺序:
(1)ActionContextCleanUp过滤器。
(2)SiteMesh核心过滤器。
(3)FilterDispatcher过滤器。