整合准备:导入jar包

如果只是访问action,没有做数据库方面的操作的话 只需要导入下面的jar

spring相关jar

以及struts相关jar包

整合过程:

用到了struts所以需要在web.xml中配置过滤器 ,又因为使用到了spring的监听器来提高性能,所以也需要配置监听器

web.xml代码:

 <?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置struts过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.mycompany.myapp.actions</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param>
</web-app>

在src下分别创建struts.xml和applicationContext.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="default" extends="struts-default" namespace="/">
<!--之前的做法:<action name="user" class="action的类路径"></action>-->
<!-- 把创建action对象交给spring进行管理 所以这里不用重新创建
在spring配置中已经创建了这里将要创建的action对象
所以只需要指定bean.xml中的创建的id值 class属性中指定的是bean.xml中创建的对象的id值-->
<action name="user" class="user"></action>
</package>
</struts>
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- spring容器创建action对象 struts配置文件中就不需要重新创建了 只需要指定id值就行 -->
<bean id="user" class="org.action.UserAction" scope="prototype"></bean>
</beans>

注意这里 action中的class属性的值和spring配置文件中的id值对应

action代码:

 package org.action;

 import javax.servlet.ServletContext;

 import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("action。。。。。。。");
ServletContext s=ServletActionContext.getServletContext();
ApplicationContext ac=(ApplicationContext) s.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if(ac!=null){
System.out.println("服务器启动时创建了applicationContext对象.........");
}
return NONE;
} }

启动服务器:

控制台打印:

请求action时的过程:

请求action时,会根据请求的action名称 去struts配置中找到与之对应的id值
找到之后不需要重新创建action对象了 因为创建对象的交给了Spring管理
加载Spring配置文件的时候 如果其中有对象的配置 那么此时就会创建配置对象
而一般为了提高性能 在服务器启动的时候就去加载Spring配置文件(利用Spring监听器实现) 创建在其中配置的对象 并且放在域对象中
所以 这里就会根据struts配置文件中对应id值后的class属性(是Spring配置文件的id值 不是action类路径)的值 去Spring配置文件中找到与之对应的id值
这样配置的前提当然是需要导入Spirng与struts整合jar包
【struts2-spring-plugin-2.3.30.jar】

spring和struts整合的更多相关文章

  1. Spring与Struts整合

    Spring框架是一个非常优秀的轻量级Java EE容器,Spring框架是整个轻量级Java EE框架的核心.大部分的Java EE应用,都会考虑使用Spring容器管理应用中的组件,从而保证各组件 ...

  2. spring+hibernate+struts整合(2)

    spring和struts2的整合 1:配置Web.xml文件 <filter> <filter-name>struts2</filter-name> <fi ...

  3. spring+hibernate+struts整合(1)

    spring+hibernate:整合 步骤1:引入类包 如下图:这里是所有的类包,为后面的struts整合考虑

  4. spring、struts整合

    package com.hanqi.test; public class JISQ { public double add(double a,double b) { return (a+b); } } ...

  5. spring 和 struts 整合遇到的问题(学习中)

    一大早就报错 org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.eng ...

  6. jbpm与spring hibernate struts整合

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  7. 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)

    轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...

  8. Spring与Struts框架整合

    Spring与Struts框架整合 Struts,用Action处理请求 Hibernate,操作数据库 Spring,负责对象创建 Spring与Struts框架整合的关键点在与:让Struts框架 ...

  9. Spring+Hibernate+Struts(SSH)框架整合

    SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...

随机推荐

  1. WPF带cookie get/post请求网页,下载文件,图片,可保持会话状态

    直接写成啦一个MyNet.cs类方便使用 get/post方法请求 //get请求 MyNet.SendRequest("http://www.baidu.com"); //pos ...

  2. 关于android中线程,进程,组件,app的理解

    android系统是一座房子.有一个正常执行的公司进驻这所座子 cpu是这家公司的老板 进程是公司中的办公室,办公室不干活 线程是办公室中的员工,干活的永远是员工 一间办公室中可有多个员工,而且办公室 ...

  3. Github 协同开发

    ithub开发流程 Github的流程.也就是: 开发者各自fork项目的repo到自己Github账户下 每次开发同步到项目的repo然后再进行开发 push自己的开发分支到自己Github账户下面 ...

  4. php---依赖倒转(反转控制)原则

    一.简介 依赖注入和控制反转说的实际上是同一个东西,它们是一种设计模式,这种设计模式用来减少程序间的耦合 优点:使用依赖注入,最重要的一点好处就是有效的分离了对象和它所需要的外部资源,使得它们松散耦合 ...

  5. GObject调用父类函数

    最近在分析Gstreamer的代码时,发现GstPipeline中有如下代码: result = GST_ELEMENT_CLASS (parent_class)->change_state ( ...

  6. Gitlab 灾备措施

    Gitlab创建备份 使用Gitlab一键安装包安装Gitlab非常简单,同样的备份恢复与迁移也非常简单.使用一条命令即可创建完整的Gitlab备份: gitlab-rake    gitlab:ba ...

  7. 第6章 服务模式 在 .NET 中实现 Service Interface

    上下文 您 的应用程序部署在 Microsoft Windows? 操作系统上.您决定将应用程序的某一块功能作为 ASP.NET Web Service 公开.互操作性是一个关键问题,因此您无法使用仅 ...

  8. (转)vue router 如何使用params query传参,以及有什么区别

    写在前面: 传参是前端经常需要用的一个操作,很多场景都会需要用到上个页面的参数,本文将会详细介绍vue router 是如何进行传参的,以及一些小细节问题.有需要的朋友可以做一下参考,喜欢的可以点波赞 ...

  9. 4185 Oil Skimming 最大匹配 奇偶建图

    题目大意: 统计相邻(上下左右)的‘#’的对数. 解法: 与题目hdu1507 Uncle Tom's Inherited Land*类似,需要用奇偶建图.就是行+列为奇数的作为X集合,偶尔作为Y集合 ...

  10. ES: 机器学习、专家系统、控制系统的数学映射

    一.基本定义    1.机器学习维基定义:机器学习有下面几种定义: "机器学习是一门人工智能的科学,该领域的主要研究对象是人工智能,特别是如何在经验学习中改善具体算法的性能". & ...