struts 1.x spring 整合

本文介绍如何通过DelegatingActionProxy实现Struts与Spring的集成,包括配置struts-config.xml和applicationContext.xml文件,以及使用请求委托提高路径映射的直观性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<struts-config>
<data-sources />
<form-beans>
<form-bean name="studentForm" type="com.ssh.form.StudentForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/student"
type="org.springframework.web.struts.DelegatingActionProxy"

parameter="cmd"
name="studentForm"
scope="request">
<forward name="loadadd" path="/student_add.jsp" />
<forward name="add" path="/student.do?cmd=list" redirect="true" />
<forward name="loadedit" path="/student_edit.jsp" />


</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation" value="
classpath:applicationContext-*.xml"/>
</plug-in>
</struts-config>

applicationContext.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--
struts的整合 -->
<bean name="/student"
class="com.ssh.action.StudentAction">
<property name="studentDao" ref="studentDao" />
</bean>
</beans>

需要说明的是,由于spring dtd规定id不能有"/",所以我们用name定义path,并且,spring beanname要和struts-config.xml中的path一致

使用DelegatingActionProxy的好处就在于你可以用不用任何spring特定的类编写Struts Action,这个方法也有不足之处,就是不太直观,因为所有路径都映射到同一个类了

对于这种情况,spring也有解决方法,就是使用请求委托

首先,struts-config.xml增加controller

<!-- 使用请求委托 -->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>
然后,修 改我们的path定义位 <action path="/listStudentAction" type="action.ListStudentActionAction"/>

这样,又和我们单独使用struts的时候一样了,但内部还是让spring取代理我们的真正的action

需要说明的是,这里的type其实是个摆设,完全可以使用 <action path="/listStudentAction"/>,写上是为了解决我们上面提到的不够直观的的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值