当Struts和Spring框架进行整合时,原本由action实例化对象的过程移交给spring来做(这个过程依赖一个叫struts2-spring-plugin的jar包,这个包主要的功能就是实现刚才那句话,但是MyEclipse自动生成Spring框架时并没有这个jar包,所以需要手动下载后导入)。

  把这个包打开会发现有一个叫struts-plugin.xml的配置文件

struts-plugin.xml

 1 <struts>
2 <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
3
4 <!-- Make the Spring object factory the automatic default -->
5 <constant name="struts.objectFactory" value="spring" />
6
7 <constant name="struts.class.reloading.watchList" value="" />
8 <constant name="struts.class.reloading.acceptClasses" value="" />
9 <constant name="struts.class.reloading.reloadConfig" value="false" />
10
11 <package name="spring-default">
12 <interceptors>
13 <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
14 </interceptors>
15 </package>
16 </struts>

这个配置文件中加了<constant name="struts.objectFactory" value="spring" />,所以就不需要在struts.xml中再次添加了。

ApplicationContext.xml

1 ...
2 <bean id="userAction" class="action.UserAction" scope="prototype">
3 <property name="userDAO" ref="userDAO" />
4 </bean>
5 ...

这里需要注意一下,再Spring配置文件中一定要加上scope这个属性,取值为’prototype’。首先得知道Spring框架生成userAction是单例的,而Structs2框架是多例的,也就是说struts2每请求一个action就会实例化一个action对象,但是Spring只会实例化一次(也就是第一次的数据),这就会导致当多次请求时会出现数据错误的情况。而当我们把scope值设为’prototype’之后,Spring就会跟struts2一样,多少次请求就实例化多少个action对象,互不影响,数据也不会出错。

struts.xml

1 ...
2 <action name="login" class="userAction" method="login">
3 <result name="admin">/main.jsp</result>
4 <result name="reader">/reader.jsp</result>
5 <result name="error">/error.jsp</result>
6 <result name="input">/userLogin.jsp</result>
7   </action>
8 ...

struts.xml中action标签中的class就可以直接写ApplicationContext.xml中bean的id值了,由Spring容器来实例化,但还是由Struts2来管理,因为ApplicationContext.xml中设置了scope属性,也不用考虑数据出错的情况。

Spring与Struts2整合时action自动注入的问题的更多相关文章

  1. spring和hibernate整合时无法自动建表

    在使用spring整合hibernate时候代码如下: <property name="dataSource" ref="dataSource" /> ...

  2. spring和hibernate整合时设置自动生成数据库的表

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFa ...

  3. spring与struts2整合出现错误HTTP Status 500 - Unable to instantiate Action

    在进行spring和struts2整合的时候因为大意遇到了一个问题,费了半天神终于找到了问题所在,故分享出来望广大博友引以为戒!! 我们都知道在spring和struts2整合时,spring接管了a ...

  4. Spring与Struts2整合VS Spring与Spring MVC整合

    Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...

  5. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  6. spring和mybatis整合时Access denied for user '***'@'localhost' (using password: YES)错误的解决方案

    参考文章:博客园文章 参考解决办法: 将数据库配置文件格式 key=value 改为 jdbc.key=value 以下为问题分析 使用Spring + Mybatis + Mysql整合时,测试报错 ...

  7. Spring Ioc源码分析系列--自动注入循环依赖的处理

    Spring Ioc源码分析系列--自动注入循环依赖的处理 前言 前面的文章Spring Ioc源码分析系列--Bean实例化过程(二)在讲解到Spring创建bean出现循环依赖的时候并没有深入去分 ...

  8. Spring与Struts2整合

    Spring与Struts2为什么要整合呢? 把Action实例交给Spring来管理!! 1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题 we ...

  9. Spring容器是如何实现 Bean 自动注入(xml)

    入口web.xml web.xml 配置文件 <!-- Spring Config --> <listener> <listener-class>org.sprin ...

随机推荐

  1. node.js安装及环境配置超详细教程【Windows系统安装包方式】

    文章目录 Step1:下载安装包 Step2:安装程序 Step3:查看 Step4:环境配置 最后补充: Step1:下载安装包 https://nodejs.org/zh-cn/download/ ...

  2. Android开发案例 点击按钮出现 简易的消息提示框

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  3. Dapper的封装、二次封装、官方扩展包封装,以及ADO.NET原生封装

    前几天偶然看到了dapper,由于以前没有用过,只用过ef core,稍微看了一下,然后写了一些简单的可复用的封装. Dapper的用法比较接近ADO.NET所以性能也是比较快.所以我们先来看看使用A ...

  4. Log4j2进阶使用(Pattern Layout详细设置)

    1.进阶说明 通过配置Layout打印格式化的日志, Log4j2支持很多的Layouts: CSV GELF HTML JSON Pattern Serialized Syslog XML YAML ...

  5. FastStoneCapture屏幕截图软件

    1.简介 FastStone Capture(FSCapture)是经典的屏幕截图软件, 可以捕捉全屏图像.活动窗口.任意指定截图形状, 而且还有图像编辑和屏幕录制功能, 还能支持屏幕放大镜和屏幕取色 ...

  6. LDAP客户端安装

    安装环境: 10.43.159.7 客户端 使用ldap客户端验证登陆: 用户为10.43.159.9服务端上面创建的ldap:zdh1234 1.安装LDAP client认证需要的pam包 yum ...

  7. Linux架构中代理服务器配置与负载均衡

    本期内容概要 代理 负载均衡 内容详细 1.代理 1.主要作用: 将流量平均分配 2.代理的方式 01 正向代理 外部想要访问服务器 先找代理 找到之后还需要找服务器 应用:VPN 02 反向代理 外 ...

  8. .NET C#教程初级篇 1-1 基本数据类型及其存储方式

    .NET C# 教程初级篇 1-1 基本数据类型及其存储方式 全文目录 (博客园).NET Core Guide (Github).NET Core Guide 本节内容是对于C#基础类型的存储方式以 ...

  9. docker的安装以及使用命令

    docker的安装 安装docker https://docs.docker.com/install/linux/docker-ce/centos/#set-up-the-repository 从 2 ...

  10. Android官方文档翻译 八 2.1Setting Up the Action Bar

    Setting Up the Action Bar 建立Action Bar This lesson teaches you to 这节课教给你 Support Android 3.0 and Abo ...