Spring和Struts整合的三种方式。
1.使用Spring的ActionSupport
2.使用Spring的DelegatingRequestProcessor类。
3.全权委托。
无论用那种方法来整合第一步就是要为Struts来装载Spring的应用环境。就是在Struts中加入一个插件。
struts-config.xml中为spring的应用环境配置插件:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml">
</plug-in>
这里spring的配置文件被作为参数配置进来。这样就不用在web.xml文件中进行配置spring.注意:确保你的applicationContext.xml在WEB-INF目录下面
我具体实现第三种spring和struts整合的方法:
我们需要将Struts动作管理委托给spring。可以通过在struts-config.xml动作映射中注册一个代理来实现(org.springframework.web.struts.DelegatingActionProxy)。代理负责在spring环境中查代Struts动作。由于动作在Spring的控制之下,所以它可以填充动作的javaBean属性,并为应用诸如Spring的AOP拦截器之类的特性带来了可能。
在注册Action的时候只注册Spring代理类的名称,而不是声明动作的类名。
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="success" path="/WEB-INF/pages/success.jsp">
<forward name="err" path="/WEB-INF/pages/err.jsp">
</action>
DelegatingActionProxy类使用动作映射名称查找Spring环境中的动作!
接着我们在Spring环境中注册一个Struts动作:
<beans>
<bean name="/login" class="com.accp.actions.LoginAction"></bean>
</beans>