Spring集成Struts主要目的是为Action注入成员,这就要让Action也纳入Spring的Ioc容器的管理。Action最终成为Spring中的Bean。
Spring的实现方案是代理Struts的Action生成过程。Struts中所有Action的type属性都设置为org.springframework.web.struts.DelegatingActionProxy,这样对应Action的生成就都交给Spring来管理了。
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm" scope="request"> <forward name="success" path="/success.jsp" /> </action>
注意type属性改为了Spring的Action代理。
<bean name="/login" class="com.iteedu.actions.LoginAction" scope="prototype"> <property name="userManager" ref="userManager" /> </bean>
name属性要和Struts的path对应。
class为Action的类路径。
scope设置为prototype,每次都重新生成,不会有线程安全问题。
这样向Action注入属性就可以成功了。