目的:spring容器管理Action类,代替Servlet

步骤:主要在配置文件

Struts2:

添加支持spring的jar包,

配置<action class="Action类在容器中的id"

Action类: 
定义需要容器注入的属性,也就是定义service,service层也要添加调用DAO的属性。并生成get和set方法。

Action:

service:

DAO:

//模拟数据库连接
private String conn;

spring:

1.web.xml配置文件:

alt+/ C 选择ContextLoadListener创建配置
配置文件的位置和名称
classpath:spring文件名.xml
加载容器的监听器

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value> </context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  

2.添加Action类的bean:
注入Action类的属性
scope=“prototype” 多例模式

<!-- DAO -->
<bean id="testDAO" class="com.hanqi.test.TestDAO">
<property name="conn" value="Oracle"></property>
</bean> <!-- service -->
<bean id="testService" class="com.hanqi.test.TestService">
<property name="testDAO" ref="testDAO"></property>
</bean> <!-- Action -->
<!-- scope="prototype"多利模式,Action类的实例不能是单利的 -->
<bean id="testID" class="com.hanqi.test.TestAction" scope="prototype">
<property name="testService" ref="testService"></property>
</bean>

  

Spring和Struts2整合的更多相关文章

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

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

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

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

  3. Spring与Struts2整合

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

  4. Spring与Struts2整合时action自动注入的问题

    当Struts和Spring框架进行整合时,原本由action实例化对象的过程移交给spring来做(这个过程依赖一个叫struts2-spring-plugin的jar包,这个包主要的功能就是实现刚 ...

  5. Spring注解与Spring与Struts2整合

    @Component @Controller @Service @Repository 四大注解作用基本一样,只是表象在不同层面 @Resource @Scope Struts2与Spring整合:1 ...

  6. spring+hibernate+Struts2 整合(全注解及注意事项)

    最近帮同学做毕设,一个物流管理系统,一个点餐系统,用注解开发起来还是很快的,就是刚开始搭环境费了点事,今天把物流管理系统的一部分跟环境都贴出来,有什么不足的,请大神不吝赐教. 1.结构如下 2.jar ...

  7. spring与struts2整合出现常见错误

    错误信息 严重: Exception starting filter struts2 Unable to load configuration. - bean - jar:file:/F:/Strut ...

  8. Spring+Hibernate+Struts2整合之实现登录功能

    前端代码: <form id="loginForm" action="${ pageContext.request.contextPath }/user_login ...

  9. Spring学习6-Spring整合Struts2

    一.Spring为什么要整合Struts2     Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了s ...

随机推荐

  1. 【经验】在CSS中定义超链接样式a:link、a:visited、a:hover、a:active的顺序

    以前用CSS一直没有遇到过这个问题,在最近给一个本科同学做的项目里面.出现一些问题,搜索引擎查了一些网站和资料,发现很多人问到这个问题,给出的结果我试了试,大部分都不正确. 给出我试的顺序,可能会对大 ...

  2. php之登录功能实现。

    项目默认存在的东西:jquery库[jquery.min.js] 登录功能实现的基本逻辑: 1.书写前台php功能基本页面:(index.php) a.编写基本功能,比如用户名.密码.登录 b.引用j ...

  3. word20161209

    failback / 故障回复 failback policy / 故障回复策略 failed / 失败 failover / 故障转移 failover policy / 故障转移策略 failov ...

  4. Python自动化之django视图

    视图 1.获取用户请求数据 request.GET request.POST request.FILES PS: GET:获取数据 POST:提交数据 request其他方法详解:http://dja ...

  5. loadrunner11录制无法打开IE浏览器

    安装loadrunner折腾了很久,后来发现是ghost系统问题.重装了系统,再重装了自己需要用的工具~ 接着学习loadrunner,一边看帮助文档一遍学.可是为嘛按照帮助文档的步骤来,就是会碰到一 ...

  6. python logging

    参考: https://docs.python.org/2/howto/logging.html#logging-basic-tutorial https://docs.python.org/2/li ...

  7. 倒计时simple 天时分秒

    new Date()new Date(yyyy,mth,dd,hh,mm,ss); //月从0计数 .getTime()返回的是一个long型的毫秒数 毫秒转成 秒 分 时 天 <div id= ...

  8. Android笔记:多线程

    定义线程的两个方法: 1. class MyThread extends Thread { public void run() {// 处理具体的逻辑 } } new MyThread().start ...

  9. 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...

  10. 如何快速的解决Maven依赖冲突

    为什么会出现依赖冲突 首先要说明Maven的依赖管理,具体的可以参考这边 Maven学习——依赖管理 这篇文章,maven在依赖冲管理中有一下几个原则. 依赖是使用Maven坐标来定位的,而Maven ...