struts 1.x spring 整合

来源:互联网 发布:临汾行知学校 编辑:程序博客网 时间:2024/05/18 04:00

<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"/>,写上是为了解决我们上面提到的不够直观的的问题