how2j.cn

下载区
文件名 文件大小
struts_spring.rar 16m

工具版本兼容问题
整合Struts和Spring的基本思路是将Action生命周期由原本的Struts进行管理,交由Spring进行管理

相当于李思思以前要自己接客,现在改成先由老鸨去招揽,回头再交给李师师进行具体的业务

以 struts模块的iterator标签为基础,进行整合



6分26秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器

步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : struts.xml   
步骤 4 : applicationContext.xml   
步骤 5 : 配置web.xml   
步骤 6 : struts2-spring-plugin-2.2.3.1.jar   
步骤 7 : 测试   

步骤 1 :

先运行,看到效果,再学习

edit
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。

测试地址

http://127.0.0.1:8080/struts_spring/listProduct
先运行,看到效果,再学习
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
在struts.xml中指定objectFactory对象工厂为spring
即action的创建交由spring进行
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.objectFactory" value="spring"/> <package name="basicstruts" extends="struts-default"> <action name="showProduct" class="productActionBean" method="show"> <result name="show">show.jsp</result> </action> <action name="addProduct" class="productActionBean" method="add"> <result name="show">show.jsp</result> </action> <action name="listProduct" class="productActionBean" method="list"> <result name="list">list.jsp</result> </action> </package> </struts>
步骤 4 :

applicationContext.xml

edit
在WEB-INF目录下,创建文件applicationContext.xml
配置productActionBean
注:不要放在src下面,一定要放在WEB-INF目录下
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="productActionBean" class="com.how2java.action.ProductAction"> </bean> </beans>
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <bean name="productActionBean" class="com.how2java.action.ProductAction">

    </bean>
</beans>
指定Context监听器,该监听器在tomcat启动的时候就会执行,用于扫描WEB-INF/lib目录下是否有 struts2-spring-plugin-2.2.3.1.jar
<web-app> <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> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
<web-app>
	<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>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>REQUEST</dispatcher>		
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 	</listener>
 	
</web-app>
步骤 6 :

struts2-spring-plugin-2.2.3.1.jar

edit
用于整合struts和spring的插件jar
本例的完整项目下载中已包含此jar
此时struts所使用的ProductAction已是由spring所创建


http://127.0.0.1:8080/struts_spring/listProduct
测试


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2020-02-19 接客问题
nicococococ

这个段子 意思太乱了。 这么说比较好 吴三桂:request 老鸨子:struts 龟公:spring 李思思:程序1 李自成:程序2 吴三桂 找 老鸨子 找 李思思 吴三桂 找 老包子 找 龟公 龟公 让李自成先上(aop) 李思思口含精页接客 龟公起到调配的作用




3 个答案

chen_jiayuan
答案时间:2024-03-26
哈哈哈哈哈,瞬间懂了

rockmanzzx
答案时间:2023-10-10
太恶心啦!

forestfire
答案时间:2021-01-15
你是真的牛bo,他妈的口含精页这种骚话也能讲出来



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2019-12-01 为什么我跑老师的项目也提示java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
sparksun007

老师的项目有这个提示但是能跑起来没问题,自己的项目也有这个提示,但是跑不起来,请问为什么?我的数据源是利用Hibernate从数据库中获取的,现在是只能struts和Spring整合,再整合Hibernate还需要其他插件是吗?




1 个答案

hz52
答案时间:2020-05-09
Spring需要的jar包都倒进来了吗



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2019-08-28 求助站长大大,Struts+Spring重定向后封装在对象中的数据依然存在
2019-04-21 求解这个说我没有add方法,可是明明就有啊
2019-04-05 例子名字前后不一


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 5 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 框架-Struts+Spring-整合步骤 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图