spring整合Struts的三种方式,及优缺点

来源:互联网 发布:扫描仪ocr软件下载 编辑:程序博客网 时间:2024/06/05 02:06

2010-04-04

前提:必须首先在struts框架中注册Spring
在struts-config.xml中的添加一个spring的插件
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
 <set-property property="contextConfigLocation"
 value="spring的xml文件的路径"
 />

</plug-in>

三种实现方式:
****1 Action===>org.springframework.web.struts.ActionSupport
同时覆盖ActionSupport的execute方法

获取spring注入的bean:
 ActionSupport.getWebApplicationContext():ApplicationContext

 ApplicationContext.getBean("dao")

缺陷:
 1)将struts的API和Spring的API耦合在一起
 2)struts的动作不在spring的管理之下,spring的优势根本没用到


****2  1)直接继承struts中的Action,将所有需要的对象,交由spring注入。即在action中提供需要的属性声明和其set方法。
    2)在Struts-config.xml中使用代理org.springframe.web.struts.DelegatingActionProxy,替代原来的Action.
 <action path="/add" type="...DelegatingActionProxy">
  <forward.....>
 </action>
    3) 在spring的配置文件中,需要额外提供具体目标Action和页面中发送到请求的对应关系。以及action需要spring注入的属性
 <bean name="和struts-config.xml中的path的值一样" class="具体的目标action">
  <property name="dao">
   <ref local="dao">
  </property>
 </bean>

优势:
 很好的发挥了spring的Ioc和Aop

****3  1) 首先使用org.springframework.web.struts.DelegatingRequestProcessor替换struts框架中的原有的RequestProcessor
   2)直接继承struts中的Action,将所有需要的对象,交由spring注入。即在action中提供需要的属性声明和其set方法。
   3)在Struts-config.xml中使用原有的action
 <action path="/add" >
  <forward.....>
 </action>
   4)在spring的配置文件中,需要额外提供具体目标Action和页面中发送到请求的对应关系。以及action需要spring注入的属性
 <bean name="和struts-config.xml中的path的值一样" class="具体的目标action">
  <property name="dao">
   <ref local="dao">
  </property>
 </bean>

    缺陷:
 1 可读性、维护性较差
 2 灵活性较差
   

 

 

异常分类:
 运行时异常(已检查异常 FileNotFoundException)
  要求必须处理(捕获、抛出)
 非运行时异常(未检查异常 NullPointerException)

 Spring中的异常是org.springframework.dao.DataAccessException

  

 

原创粉丝点击