Spring与Struts的集成(第二种方案):
原理:将业务逻辑对象通过spring注入到Action对象中,从而避免在Action对象中的查找代码,能够将
Action完全跟spring解耦
1、创建web项目
2、将spring与web进行集成
3、安装struts
* 拷贝相关的类库(struts和jstl等)
* 修改web.xml,定义struts的ActionServlet
* 提供struts配置文件
* 添加国际化处理的资源文件
4、Action的写法:
* 因为Action类需要调用业务逻辑类,所以直接定义一个业务逻辑类的实例变量
* 给这个变量定义一个setter方法(主要是为了让spring能够通过设值方法注入这个业务逻辑对象)
5、Action的配置
* action配置中的type应该修改为:org.springframework.web.struts.DelegatingActionProxy
6、在spring配置文件中配置Action类,如:
<bean name="/login" class="com.bjsxt.web.actions.UserAction" scope="prototype">
<property name="userManager" ref="userManager"></property>
</bean>
* 需要注意:
- 必须使用name来作为bean的名称
- 必须让name的值与action配置中的path保持一致
- 必须注入需要用到的Manager对象
- 最好能将Action对象定义为prototype的scope,即每次获取这个对象的时候,都创建新的对象,
这样可以避免struts action的线程安全的问题!