spring和struts1.x的整合方法大致分为三种:
首先让beanFactory在启动时就创建,并且指定spring的配置文件名,在web.xml中添加
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
1.使用插件的形式,将action交给spring来管理
在struts-config.xml中添加
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
action中的配置改为(修改type属性)
<action path="/run"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="success" path="/ok.jsp" />
</action>
然后在applicationContext.xml中配置action
<bean name="/run" class="com.che.action.UserAction" >
<property name="iservice">
<ref local="service" />
</property>
</bean>
2.和上一种方法差不多,不用插件形式在struts-config.xml中加
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
action中的配置和以前一样
<action path="/run"
type="com.che.action.UserAction">
<forward name="success" path="/ok.jsp"/>
</action>
applicationContext.xml中对象的action配置不变
<bean name="/run" class="com.che.action.UserAction">
<property name="iservice">
<ref local="service" />
</property>
</bean>
3.使用继承DispatchActionSupport的方法
将自己的action继承DispatchActionSupport,而不继承action
struts-config.xml文件的配置和以前的一样
applicationContext.xml文件中也不用配置相应的action
但是每次要得到所依赖的对象时,只能通过getWebApplicationContext().getBean()方法来得到对象