目的: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. Fragment应用总结

    1.FrameLayout 常用于作为Android自带组件的父节点 2.Fragment就是一个普通的Java类,用Android.app这个包         Fragment也是一个ListVi ...

  2. Sublime编辑器安装使用

    用习惯了VS2010强大的IDE工具,但也被它折腾过.烦恼过,当vs加载超过万行的脚本代码时,界面半天才反应,经常卡死,电脑配置决定算得上顶呱呱. 不喜欢逆来顺受,于是选择了txt文本编辑器,最原始的 ...

  3. StartUML反向(逆向)Java工程通过代码生成类图

     在软件工程中,通过都是先了详细设计,然后按照详细设计来进行开发.在编写详细设计的时候,通常都会画一些类图.时序图.流程图等等UML设计,然后通过uml类图生成代码,这个属于正向工程生成代码,然而在实 ...

  4. opencv的图片的灰度处理‘

    #include "stdafx.h" //实现将彩色图片转换成灰度图 int _tmain(int argc, _TCHAR* argv[]){ IplImage *image; ...

  5. Rails 实现多对多自连接

    1. 先生成关联代码 class WorkConnect < ActiveRecord::Base belongs_to :working, class_name: "Company& ...

  6. java9

    1:StringBuffer(掌握) (1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了 一个字符串缓冲区类.StringBuffer供我们使 ...

  7. server2003中看不到网上邻居内容,其他电脑无法通过计算机名和IP访问本计算机(但网上邻居中可访问到)

    现象1:server2003中看不到网上邻居内容,查看工作组计算机看到的是空列表, 现象2:其他电脑无法通过计算机名和IP访问本计算机(但网上邻居中可访问到)   访问提示:--Windows 200 ...

  8. 关于kali2.0 rolling无法连接数据的解决办法

    在使用kali2.0时,经常遇到metasploit无法连接数据库的问题,经过在网上寻找资料,很多都是对kali2.0的,有些路径都是不同的了,所以未能解决, 最后在自己的摸索下,找到了如下方法: 其 ...

  9. javascript 核心语言笔记- 3 - 类型、值和变量

    JavaScript 中的数据类型分为两类:原始类型(primitive type)和对象类型(object type).原始类型包括数字.字符串和布尔值 JavaScript 中有两个特殊的原始值: ...

  10. java类读取properties文件

    package com.bshinfo.el.userInfo.util; import java.io.BufferedReader;import java.io.File;import java. ...