spring和struts整合
整合准备:导入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整合的更多相关文章
- Spring与Struts整合
Spring框架是一个非常优秀的轻量级Java EE容器,Spring框架是整个轻量级Java EE框架的核心.大部分的Java EE应用,都会考虑使用Spring容器管理应用中的组件,从而保证各组件 ...
- spring+hibernate+struts整合(2)
spring和struts2的整合 1:配置Web.xml文件 <filter> <filter-name>struts2</filter-name> <fi ...
- spring+hibernate+struts整合(1)
spring+hibernate:整合 步骤1:引入类包 如下图:这里是所有的类包,为后面的struts整合考虑
- spring、struts整合
package com.hanqi.test; public class JISQ { public double add(double a,double b) { return (a+b); } } ...
- spring 和 struts 整合遇到的问题(学习中)
一大早就报错 org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.eng ...
- jbpm与spring hibernate struts整合
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...
- Spring与Struts框架整合
Spring与Struts框架整合 Struts,用Action处理请求 Hibernate,操作数据库 Spring,负责对象创建 Spring与Struts框架整合的关键点在与:让Struts框架 ...
- Spring+Hibernate+Struts(SSH)框架整合
SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...
随机推荐
- ZOJ 1654 Place the Robots(最大匹配)
Robert is a famous engineer. One day he was given a task by his boss. The background of the task was ...
- 【cl】找不到火狐Cannot find firefox binary in PATH
org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is ins ...
- VC++玩转Native Wifi API 2---Wifi on与wifi off
有心栽花花不开,无心插柳柳成排. 今天要说的这个wifi on\off是在软件层面控制无线网卡的开和关. 问题听起来简单,调查起来复杂.但解决起来却也简单.关键函数便是Native wifi a ...
- 怎样使用Eclipse PDT调试PHP程序
本文主要介绍的是怎样用eclipse pdt调试PHP 代码. 1. 下载eclipse.从官网上找就能够了,并确认当前系统中有java环境,即jdk和jre. 2. 安装pdt了,採用的是在线安装. ...
- friend(hdoj 1719)
Friend Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- VueJS 开发常见问题集锦
由于公司的前端开始转向 VueJS,最近开始使用这个框架进行开发,遇到一些问题记录下来,以备后用. 主要写一些 官方手册 上没有写,但是实际开发中会遇到的问题,需要一定知识基础. 涉及技术栈 CLI: ...
- 使用DbVisualizer变量
变量可用于构建参数化SQL语句,并让DbVisualizer在执行SQL时提示您输入值.如果您重复执行相同的SQL,只是希望在同一个SQL语句中传递新数据,这很方便. 变量语法 变量格式支持设置默认值 ...
- SQL Server将数据导出到SQL脚本文件
http://www.studyofnet.com/news/list-8883.2-1-4.html 一.SQL Server 2008将数据导出到SQL脚本文件 1.打开SQL Server200 ...
- Python快速定位工作目录
原文链接:http://www.cnblogs.com/wdong/archive/2010/08/19/1802951.html 常年奋斗在编码一线的同学,应该都深有体会,工作久了,很多项目文件.技 ...
- SIFT算法总结:用于图像搜索
原始文章链接:http://bubblexc.com/y2011/163/ 原文链接:http://blog.csdn.net/cserchen/article/details/5606859 关于三 ...