Struts2和Spring都是不错的开源框架,Spring与Struts2集成开发,把二者结合在一起使用,开发效果更佳,效率杠杠的。下面介绍一下如何将Spring与Struts2集成在一起开发。分七步战略:
1、添加Struts2 支持jar包  ;注意:加上一个Struts-spring-plugin(集成)插件包
2、添加spring 支持jar包
3、编写struts.xml配置文件
注意:Action是交由Spring管理;所以action class属性应和spring 中配置的Action Bean ID一致
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<!-- 配置用户ACTION
Action由Spring维护
class就是spring中Action Bean的ID
-->
<action name="useraction*" class="useraction" method="{1}">
<result name="success" type="redirect">success.jsp</result>
<result name="login" type="redirect">index.jsp</result>
</action> </package>
</struts>
4、在web.xml中配置struts2过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置struts2核心过滤器 -->
<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> <!-- 指定spring配置文件所在位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置sprig监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


5、编写spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 配置持久化层 new userdao()-->
<bean id="userdao" class="DAO.impl.UserDaoimpl" scope="prototype"></bean> <!-- 创建业务层 new userbiz() -->
<bean id="userbiz" class="BIZ.impl.UserBizimpl" scope="prototype">
<!-- 注入持久化层 DAO
IUuserDao userdao = new userdao()
-->
<property name="userdao" ref="userdao"></property>
</bean> <bean id="useraction" class="Action.UserAction" scope="prototype">
<!-- 注入业务层BIZ
IUserBiz userbiz = new UserBiz()
-->
<property name="userbiz" ref="userbiz"></property>
</bean> </beans>

6、在web.xml中配置spring监听器和指定配置文件所在位置(注:如果其文件在WEB-INF/applicatonContext.xml下的话就不需要指定)
    <!-- 指定spring配置文件所在位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置sprig监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

7、编写三层结构;通过spring依次注入各依赖对象
BIZ 中注入 DAO层
Action 层中注入 BIZ层

Spring与Struts2集成开发的更多相关文章

  1. Eclipse 下Spring cloud项目集成开发插件Spring Tool Suite (STS) 安装

    安装eclipse插件 Help->Eclipse Marketplace-> Search中查找"Spring Tool Suite (STS) for Eclipse&quo ...

  2. 从Spring看Web项目开发

    之前简单介绍过Spring框架,本文换个角度重新诠释Spring.使用Java语言开发的项目,几乎都绕不过Spring,那么Spring到底是啥,为何被如此广泛的应用,下面从以下两个问题出发来剖析Sp ...

  3. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(二)——Struts2集成

    1. pom.xml文件添struts2依赖jar包: <!-- 与Struts2集成必须使用 --> <dependency> <groupId>org.spri ...

  4. 【SSH】---【Struts2、Hibernate5、Spring4集成开发】【SSH框架整合笔记】

    Struts2.Hibernate5.Spring4集成开发步骤: 一.导入Jar包(基本的大致有41个,根据实际项目的需求自己添加) antlr-2.7.7.jar aopalliance.jar ...

  5. Struts2、Spring、Hibernate 高效开发的最佳实践(转载)

    Struts2.Spring.Hibernate 高效开发的最佳实践 Struts2.Spring.Hibernate(SSH)是最常用的 Java EE Web 组件层的开发技术搭配,网络中和许多 ...

  6. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(三)——H2,MyBatis集成

    1.配置h2,连接池,MyBatis Maven依赖: <!-- spring与数据库访问集成(非Hibernate) --> <dependency> <groupId ...

  7. Spring 集成开发工具(STS)安装及配置

    安装 spring 集成开发工具,下载地址:https://spring.io/tools 下载后,解压,双击 STS ,运行. 如果提示: 去oracle的网站上下载了1.8版本的jdk,下载地址如 ...

  8. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  9. 【超全整理】J2EE集成开发环境MyEclipse使用心得汇总

    一.首先我们为什么需要MyEclipse? 下面允许我做一些简要的介绍: 应该大家都知道另一个MyEclipse的近亲——Eclipse的优点:免费.程序代码排版功能.有中文汉化包.可增设许多功能强大 ...

随机推荐

  1. php删除数组中指定值的元素

    php删除数组中指定值的元素 /** * 删除数组中指定值的元素 * @author: ibrahim * @param array $arr 数组 * @param string $val 值 * ...

  2. 深度学习-theano-windows -cuda-环境搭建

    本文将具体介绍深度学习之cuda的环境搭建 工具:支持CUDA的显卡(安装cuda6.5),VS2013.Anaconda. 步骤: 1.安装cuda6.5 这个不具体介绍,网上有很多文章.注意选择你 ...

  3. netty4与protocol buffer结合简易教程

    各项目之间通常使用二进制进行通讯,占用带宽小.处理速度快~ 感谢netty作者Trustin Lee.让netty天生支持protocol buffer. 本实例使用netty4+protobuf-2 ...

  4. oracle 11g GRID 中 关于 OLR 须要知道的一些内容

     oracle 11g GRID 中 关于 OLR 须要知道的一些内容 1.检查olr 的状态: [root@vmrac1 ~]# ocrcheck -local Status of Oracle ...

  5. Python爬虫【第3篇】【多线程】

    一.多线程 Python标准库提供2个模块,thread是低级模块,threading是高级模块 1.threading模块创建多线程 方式1:把1个函数传入并创建Thread实例,然后调用start ...

  6. salt-stack "No Top file or external nodes data matches found"解决

    salt-stack在配置分组时提示如下信息: No Top file or external nodes data matches found 后来在官网上找到如下提示,意思是需要重启master服 ...

  7. 图像处理之基础---滤波器之高斯低通滤波器的高斯模板生成c实现

    ()代码实现 对原图进行高斯平滑,去除图像中的计算噪声void Bmp::MakeGauss(double sigma,double **pdKernel,int *pnWindowSize){ // ...

  8. web 开发之js---js 中的数组操作

    js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^var arr = new Array();arr[0] = "aaa";arr[1] ...

  9. Highcharts数据表示(3)

    Highcharts数据表示(3) 採用对象数组的形式,能够明白节点上每一个项目的值.可是当节点较多时,会造成大量的冗余代码. 假设不写配置项名称,对象数组就能够简化二维数组.形式例如以下: data ...

  10. mciSendString详解(转)

    做个mp3播放器,用realplay和WMP做出来的程序内存占用太大.如果你仅仅是播放MP3,建议使用API函数mciSendString,我把该函数的详细资料罗列如下供你参考.Option Expl ...