zoukankan      html  css  js  c++  java
  • Spring集成struts2

    Spring集成struts2

    根据Spring官方文档介绍,使用Struts的Spring Plugin 插件进行对Spring 集成

    Struts集成Spring的官方文档介绍
    http://struts.apache.org/docs/spring-plugin.html
     
    大致分为几个部分
    1说明
     
    使用spring插件来集成
    spring插件通过重写Struts的ObjectFactory(对象工厂)来增强框架核心对象的创建,它使用struts配置中class属性来和spring配置中的id属性相关联(class属性此时不是真正的类名,而是bean的id), 如果找不到关联的id,再尝试用普通方法创建(class还是当成一个类的全名来创建bean),然后使用Spring来自动装载(autowire)。创建Action时可使用Spring的Scope特性,设定Action的范围,如session,applicaiton,或自定义范围。Struts的Action默认范围是每个request一个。
    注意:使用spring来管理Action的实例并不是必须的。
    特性:
        可以使用Spring来创建Action,Interceptor和Result实例。
        struts创建的对象在创建时也可以用上Spring的autowire机制。
        如果不使用Spring的 ObjectFactory,则提供两个拦截器来自动装载各Action。
        
    2 用法
    启用Spring集成,只需要把 struts2-spring-plugin-x-x-x.jar这个jar包加入到应用就可以。
    如果启用了两个以上的Object Factory框架,则需要设置使用Spring的对象工厂。
    struts.properties
    struts.objectFactory = spring
    struts.xml
    <struts>
      <constant name="struts.objectFactory" value="spring" />
      ...
    </struts>
     
        2.1自动装载
     
    集成框架默认是自动装载的,如果需要设置,可以设置spring.autowire这个属性(自动装载即在spring的配置中寻找和对象属性名相同的对象)
    Wiring Mode
    struts.objectFactory.spring.autoWire = type
    可选项 name type auto constructor no
    struts默认使用spring创建对象,如果无法被spring创建,则自己创建。
    启用spring集成其他(除action之外的)应用内的对象需要两个步骤:
    • 配置spring监听器
    web.xml
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    • 在spring配置文件中注册对象
    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans default-autowire="autodetect">
        <bean id="personManager" class="com.acme.PersonManager" scope="prototype"/>
        ...
    </beans>
    如果需要更多配置文件:
    <!-- Context Configuration locations for Spring XML files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
    </context-param>
     
     
     
     
     
     
        2.2 用Spring初始化Action
    一般来说,在stuts.xml中配置的每个Action都会默认使用SpringObjectFactory来创建,框架会让Spring创建Action并自动装载依赖的属性。

      struts.xml 文件  Action 的class 属性需要做变更.

    struts.xml
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    <struts>
        <include file="struts-default.xml"/>
     
        <package name="default" extends="struts-default">
            <action name="foo" class="com.acme.Foo">
                <result>foo.ftl</result>
            </action>
        </package>
     
        <package name="secure" namespace="/secure" extends="default">
            <action name="bar" class="bar">
                <result>bar.ftl</result>
            </action>
        </package>
    </struts>
    <wiz_tmp_tag class="wiz-table-scroll" style="height: 17px; 1851px; overflow-x: scroll; overflow-y: hidden; position: fixed; bottom: 0px; z-index: 999; display: none; background-color: rgba(255, 255, 255, 0.2);">
     

    当你有一个在applicationContext.xml 中定义的名叫"bar"的 Spring bean。  注意 com.acme.Foo Action 不需要改变,因为它可以被自动装载。

     

     下面是一个对应的spring配置文件,在这里bar这个bean可以被查找到。

    applicationConext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans default-autowire="autodetect">
        <bean id="bar" class="com.my.BarClass" singleton="false"/>
        ...
    </beans>

    To use session-scoped components with Spring and Struts, see the Spring Session Components Workarounds analysis.

        2.3 重新装载类

    Spring插件能够被配置成可以自动重新装载被修改的java类,这个功能主要被用来实现“热部署”机制,即可以不重启整个容器就能让java的修改生效(通常用于开发阶段)

    1. 设置常量"struts.devMode" 为"true"   <constant name="struts.devMode" value="true"></constant>
    2. 设置"struts.class.reloading.watchList" 值为逗号分隔的目录,jar文件列表,绝对或相对路径都可以
    3.  web.xml中添加配置:

      <context-param>
          <param-name>contextClass</param-name>
          <param-value>org.apache.struts2.spring.ClassReloadingXMLWebApplicationContext</param-value>
      </context-param>
    4. 添加这个maven依赖(或直接增加jar包 commons-jci-fam-1.1.jar)

      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-jci-fam</artifactId>
          <version>1.0</version>
      </dependency>

     

     如果让所有类都使用重新加载机制,会可能导致ClassCastException异常,因为有多个类加载器装载相同的类无法互相分配,使用下面这个组件,来限制使用重新装载的类:例如只开action的重新装载

    <constant name="struts.class.reloading.acceptClasses" value="com.myproject.example.actions..*" />

     

     这个功能是实验性的,不要在生产环境使用

     

       2.4其他相关配置

    The following settings can be customized. See the developer guide.

    Setting

    Description

    Default

    Possible Values

    struts.objectFactory.spring.autoWire

    The autowire strategy

    name

    name,type,auto, or constructor

    struts.objectFactory.spring.autoWire.alwaysRespect

    Whether the autowire strategy should always be used, or if the framework should try to guess the best strategy based on the situation

    false for backwards-compatibility

    true or false

    struts.objectFactory.spring.useClassCache

    Whether to have Spring use its class cache or not

    true

    true or false

    struts.class.reloading.watchList

    List of jar files or directories to watch for changes

    null

    Comma separated list of absolute or relative paths to jars or directories

    struts.class.reloading.acceptClasses

    List of regular expressions of accepted class names

    null

    Comma separated list of regular expressions of classes that will be loaded by the reloading class loader(we suggest to add regular expressions so only action classes are handled by the reloading class loader)

    struts.class.reloading.reloadConfig

    Reload the runtime configuration (action mappings, results etc) when a change is detected in one of the watched directories

    false

    true or false

    struts.objectFactory.spring.enableAopSupport

    Uses different logic to construct beans to allow support AOP, it uses an old approach to create a bean, switch this flag if you have problems with Spring beans and AOP

    false

    true or false

    <wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: block;">
     
    <wiz_tmp_tag class="wiz-table-tools" contenteditable="false" style="top: 0px; display: block; position: fixed; left: 15px;">
    •  
      • 上插入行
      • 下插入行
      • 左插入列
      • 右插入列
      • 删除当前行
      • 删除当前列
    •  
       
    •  
       
    •  
      • 合并单元格
      • 拆分单元格
      • 清空单元格
    •  
      • 平均分配各列
      • 删除表格

     

    2.5 安装

     

     安装这个插件,只需直接把jar包复制到应用的 /WEB-INF/lib 目录下

     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    <c:if></c:if>用法-转载
    Windows下配置Apache服务器
    ScrureCRT访问CentOS时出现乱码的解决办法
    Windows平台下Git服务器搭建
    Group_Concat函数示例
    Mysql Federated Server 示例
    MySQL几个特别语法示例
    MySQL事件调度器
    Disruptor Java版和.NET版的区别
    委托的三种实现方式
  • 原文地址:https://www.cnblogs.com/doitwell/p/7057724.html
Copyright © 2011-2022 走看看