整合准备:导入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. 准备开源用javascript写Tomcat下的WebApp的项目

    原创文章,转载请注明. 这个想法由来已久.用javascript编写Tomcat下的WebApp.现现在也有alpha版本号的实现. 这种话,前端程序猿就能够像用Node.js那样,用javascri ...

  2. Java学习需要掌握的一些知识

    Java学习需要掌握的一些知识: <一>1.Jvm 部分Jvm 内存模型.Jvm 内存结构.Jvm 参数调优.Java 垃圾回收<二>Java 基础部分1.必须会使用 List ...

  3. Running the app on your device

    So far, you've run the app on the Simulator. That's nice and all but probably notwhy you're learning ...

  4. 有什么springMVC+myBatis的书?

    最近在看MyBatis,把我最近看的东西给你整理一下吧. 书籍:深入浅出MyBatis技术原理与实战 下载地址:[免费]深入浅出MyBatis技术原理与实战.pdf-CSDN下载 MyBatis 相关 ...

  5. B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    水题,20分钟AC,最大值最小,一看就是二分答案... 代码: Description Farmer John has built a <= N <= ,) stalls. The sta ...

  6. 备份SQL SERVER 2005数据库

  7. LINUX/UNIX找回删除的文件

    当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹.管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志.有时可以通过lsof来恢复这些 ...

  8. [JavaEE] Apache Maven 入门篇(下)

    http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-2-405568-zhs.html 作 ...

  9. 如何用ajax写分页查询(以留言信息为例)-----2017-05-17

    要写分页,首先你得清楚,一页你想显示多少条信息?如何计算总共显示的页数? 先说一下思路: (1)从数据库读取数据,以chenai表为例,读取所有留言信息.并能够实现输入发送者,可以查询该发送者的留言总 ...

  10. 使用autofac在mvc5下依赖注入

    把遇到的问题汇总一下: 一.安装mvc5版本 命令:pm> Install-Package Autofac 结果安装的Autofac.Integration.Mvc(版本为4.0),所引用的依赖 ...