这种集成方案要求我们编写一个Struts Action,但它只不过是一个包含在Spring应用上下文中的真正Struts Action的一个代理。该代理Action从Struts插件ContextLoaderPlugIn中获取应用上下文,从中查找真正的Struts Action,然后将处理委托给真正的Struts Action。这个方法的幽雅之处在于:只有代理action才会包含Spring特定的处理。真正的Action可以作为 org.apache.struts.Action的子类来编写。
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
在这种方式之下,我们的Struts Action类和Spring是低耦合的,它仅仅依赖了Spring提供的反向控制(IoC)机制把CourceService注入到了我们的Action中。
Spring提供IoC反向控制,主要需要两个步骤的配置:
(1).在struts-config.xml中注册Struts Action。但要注意的是我们在这里注册的是代理Action。幸运的是,我们不必亲自编写这个类。因为Spring已经通过 org.springframework.web.struts.DelegatingActionProxy提供了这个代理的Action。具体的配置方法如下:
<action type="org.springframework.web.struts.DelegatingActionProxy" path="/listCourses">
(2)将真正的Struts Action作为一个Spring Bean并在Spring上下文配置文件中作为一个Bean注册之。并将Action所要引用的courceService注入给它。
<bean class="com.eRedCIP.web.CourceAction" name="/listCourses">
<property name="courceService;">
<ref bean="courseService">
</property>
</bean>
注意:name属性的值是非常重要的,它必须和struts-config.xml中的path属性完全一致。这是因为 DelegatingActionProxy会使用path属性值在Spring上下文中查找真正的Action。使用 DelegatingActionProxy的好处在于我们可以不使用任何Spring特定的类来编写Struts Action。同时,Struts动作能够利用IoC取得和他合作的对象。唯一不足之处就是不太直观,配置相对复杂。