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层还 ...
随机推荐
- @RequiresPermissions 注解说明
@RequiresAuthentication验证用户是否登录,等同于方法subject.isAuthenticated() 结果为true时.@RequiresUser验证用户是否被记忆,user有 ...
- luogu2606 排列计数
题目大意 求满足下列条件的排列$P$的数量:$\forall P_i, P_i>P_{\lfloor \frac{i}{2}\rfloor}$. 思路 从下标入手 反过来想,也就是对$\fora ...
- spark 决策树分类算法demo
分类(Classification) 下面的例子说明了怎样导入LIBSVM 数据文件,解析成RDD[LabeledPoint],然后使用决策树进行分类.GINI不纯度作为不纯度衡量标准并且树的最大深度 ...
- Python使用装饰器自动调用父类__init__
众所周知,Python中class的构造函数实际是__new__(),但是如果我们执行p1=Point()的时候,不仅会调用Point的__new__方法,而且会调用Point的__init__方法. ...
- virtualbox 安装虚拟机(centos7) 并映射本地文件夹至虚拟机(增强工具)
一.安装环境 操作系统:windows10 virtualbox: 5.2.20 (在安装virtualbox 时可能需要 进入BIOS 设置虚拟化系统启动) centos7:http://mirro ...
- ios UISegmentedControl的使用简介
初始化UISegmentedControl NSArray *arr = [[NSArray alloc]initWithObjects:@"轻拍",@"长按" ...
- golang图片压缩
package main import ( //"encoding/json" "fmt" //"os" //&qu ...
- react拼接class&将JS标签转换为HTML
1.在JS中混杂字符和HTML标签,识别方法: const menuList = ['门店', '星享俱乐部', '菜单', '<hr></hr>', '星巴克移动应用', ' ...
- [Offer收割]编程练习赛42
对局匹配 直接贪心 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #i ...
- mysql和eclipse连接jdbc驱动配置
环境Windows10 eclipse 64位 MySQL 一:资料准备 (MySQL,eclipse下载安装不在赘述) 配置好MySQL环境后 下载jdbc地址http://dev.mysql.co ...